asp.net - How to read and write a file using Memory Mapped File C#? -
i have image in d drive "d:\image\1.tiff". want read file , write in location, example in path "d:\project\". how using memory mapped file?
the createfromfile methods create memory-mapped file existing file on disk. following example creates memory-mapped view of part of extremely large file , manipulates portion of it.
using system; using system.io; using system.io.memorymappedfiles; using system.runtime.interopservices; class program { static void main(string[] args) { long offset = 0x10000000; // 256 megabytes long length = 0x20000000; // 512 megabytes // create memory-mapped file. using (var mmf = memorymappedfile.createfromfile(@"c:\extremelylargeimage.data", filemode.open,"imga")) { // create random access view, 256th megabyte (the offset) // 768th megabyte (the offset plus length). using (var accessor = mmf.createviewaccessor(offset, length)) { int colorsize = marshal.sizeof(typeof(mycolor)); mycolor color; // make changes view. (long = 0; < length; += colorsize) { accessor.read(i, out color); color.brighten(10); accessor.write(i, ref color); } } } } } public struct mycolor { public short red; public short green; public short blue; public short alpha; // make view brighter. public void brighten(short value) { red = (short)math.min(short.maxvalue, (int)red + value); green = (short)math.min(short.maxvalue, (int)green + value); blue = (short)math.min(short.maxvalue, (int)blue + value); alpha = (short)math.min(short.maxvalue, (int)alpha + value); } }
the following example opens same memory-mapped file process.
using system; using system.io.memorymappedfiles; using system.runtime.interopservices; class program { static void main(string[] args) { // assumes process has created memory-mapped file. using (var mmf = memorymappedfile.openexisting("imga")) { using (var accessor = mmf.createviewaccessor(4000000, 2000000)) { int colorsize = marshal.sizeof(typeof(mycolor)); mycolor color; // make changes view. (long = 0; < 1500000; += colorsize) { accessor.read(i, out color); color.brighten(20); accessor.write(i, ref color); } } } } } public struct mycolor { public short red; public short green; public short blue; public short alpha; // make view brigher. public void brighten(short value) { red = (short)math.min(short.maxvalue, (int)red + value); green = (short)math.min(short.maxvalue, (int)green + value); blue = (short)math.min(short.maxvalue, (int)blue + value); alpha = (short)math.min(short.maxvalue, (int)alpha + value); } }
you can read more @ :http://www.codeproject.com/articles/138290/programming-memory-mapped-files-with-the-net-frame
Comments
Post a Comment