如何使用内存映射文件在C#中读写文件?

5

我有一张图片存储在D盘,路径为"D:\Image\1.tiff"。我想读取这个文件并将它写入到另一个位置,例如路径为"D:\Project\"。如何使用内存映射文件实现这个操作?

2个回答

6
我现在可以使用下面的代码通过内存映射文件实现读写文件:
FileStream stream = File.OpenRead(@"D:\FFv1\dpx1\1.dpx");
byte[] fileBytes = new byte[stream.Length];
string Output = @"D:\Vanthiya Thevan\FFv1\dpx1\2.dpx";
using (var fileStream = new FileStream(Output, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
using (MemoryMappedFile memoryMapped = MemoryMappedFile.CreateFromFile(fileStream, "MapName", fileBytes.Length,
MemoryMappedFileAccess.ReadWrite, new MemoryMappedFileSecurity(), HandleInheritability.Inheritable, true))
{
    var viewStream = memoryMapped.CreateViewStream();
    viewStream.Write(fileBytes, 0, fileBytes.Length); 
}

5

CreateFromFile方法可以从磁盘上的现有文件创建一个内存映射文件。以下示例创建了一个极大文件的内存映射视图,并对其进行操作。

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 the memory-mapped file. 
        using (var mmf = MemoryMappedFile.CreateFromFile(@"c:\ExtremelyLargeImage.data", FileMode.Open,"ImgA"))
        {
            // Create a random access view, from the 256th megabyte (the offset) 
            // to the 768th megabyte (the offset plus length). 
            using (var accessor = mmf.CreateViewAccessor(offset, length))
            {
                int colorSize = Marshal.SizeOf<MyColor>();
                MyColor color;

                // Make changes to the view. 
                for (long i = 0; i < length; i += 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 the 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);
    }
}

下面的示例打开同一内存映射文件,供另一个进程使用。
using System;
using System.IO.MemoryMappedFiles;
using System.Runtime.InteropServices;


class Program
{
    static void Main(string[] args)
    {
        // Assumes another process has created the memory-mapped file. 
        using (var mmf = MemoryMappedFile.OpenExisting("ImgA"))
        {
            using (var accessor = mmf.CreateViewAccessor(4000000, 2000000))
            {
                int colorSize = Marshal.SizeOf<MyColor>();
                MyColor color;

                // Make changes to the view. 
                for (long i = 0; i < 1500000; i += 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 the 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);
    }
}

你也可以在这里阅读更多内容:http://www.codeproject.com/Articles/138290/使用.NET框架编写内存映射文件的程序

7
这个例子来自框架文档,你可能应该注明一下。https://learn.microsoft.com/en-us/dotnet/api/system.io.memorymappedfiles.memorymappedfile - kristianp

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接