Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么在将位图源保存为PixelFormat.Format48bppRgb的位图时颜色会更改?_C#_Wpf_Bmp - Fatal编程技术网

C# 为什么在将位图源保存为PixelFormat.Format48bppRgb的位图时颜色会更改?

C# 为什么在将位图源保存为PixelFormat.Format48bppRgb的位图时颜色会更改?,c#,wpf,bmp,C#,Wpf,Bmp,我有一个.NEF图像。我安装了编解码器,然后使用以下代码显示: BitmapDecoder bmpDec = BitmapDecoder.Create(new Uri(@"C:\Temp\Img0926.nef"), BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.None); BitmapSource srs = bmpDec.Frames[0]; this.imgDisplayed4.Source = srs; this.

我有一个.NEF图像。我安装了编解码器,然后使用以下代码显示:

BitmapDecoder bmpDec = BitmapDecoder.Create(new Uri(@"C:\Temp\Img0926.nef"), BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.None);
BitmapSource srs = bmpDec.Frames[0];
this.imgDisplayed4.Source = srs;
this.imgDisplayed4.Stretch = Stretch.UniformToFill;
  • imgDisplayed4是图像控件
然后使用以下代码创建并保存bmp:

Bitmap bmp = new Bitmap(srs.PixelWidth, srs.PixelHeight, System.Drawing.Imaging.PixelFormat.Format48bppRgb);
System.Drawing.Imaging.BitmapData data = bmp.LockBits(new System.Drawing.Rectangle(System.Drawing.Point.Empty, bmp.Size), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format48bppRgb);
srs.CopyPixels(Int32Rect.Empty, data.Scan0, data.Height * data.Stride, data.Stride);
bmp.UnlockBits(data);
bmp.Save(@"C:\Temp\Images\Img0926-1.bmp");
它保存bmp文件,但是,bmp中的颜色似乎有一些变化。我附上了3个屏幕截图。第一个是Windows Photo Viewer显示的保存的bmp文件,第二个是原始的.NEF图像,第三个是图像控件中显示的图像

我们可以看到它们都是相似的。但是,第二个和第三个有相似的颜色,它们与第一个不同

我搜索了很多,我能找到的都和我正在做的相似。但是,它们都支持格式化32bpprgb。问题可能是因为我使用的图像格式为48bpprgb?有人知道吗?如何解决这个问题

谢谢


我意识到第一张图像和第二张图像的区别在于:如果我们在第一张图像中切换红色部分和B部分,那么我们得到第二张图像。因此,我将代码更改为:

System.Drawing.Imaging.BitmapData data = bmp.LockBits(new System.Drawing.Rectangle(System.Drawing.Point.Empty, bmp.Size), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format48bppRgb);

unsafe
{
    srs.CopyPixels(Int32Rect.Empty, data.Scan0, data.Height * data.Stride, data.Stride);
    for (var row = 0; row < srs.PixelHeight; row++)
    {
        for (var col = 0; col < srs.PixelWidth; col++)
        {
            byte* pixel = (byte*)data.Scan0 + (row * data.Stride) + (col * 6);

            var val1 = pixel[1];
            var val3 = pixel[3];
            var val2 = pixel[2];
            var val4 = pixel[4];
            var val5 = pixel[5];
            var val0 = pixel[0];

            //// 0, 1: B, 2:3: G, 4, 5: R
            pixel[5] = val1;
            pixel[4] = val0;

            pixel[0] = val4;
            pixel[1] = val5;


        }
    }

}
bmp.UnlockBits(data);
System.Drawing.Imaging.BitmapData data=bmp.LockBits(新的System.Drawing.矩形(System.Drawing.Point.Empty,bmp.Size)、System.Drawing.ImageLockMode.WriteOnly、System.Drawing.Imaging.PixelFormat.Format48bppRgb);
不安全的
{
srs.CopyPixels(Int32Rect.Empty,data.Scan0,data.Height*data.Stride,data.Stride);
对于(var row=0;row
现在,结果是正确的

当PixelFormat为48bpprgb时,BitmapSource.CopyPixels中似乎存在错误。它以BGR而不是RGB的顺序复制像素

有人知道我为什么要换R&B部分吗?还有其他建议吗

不管怎样,它现在很好用。我花了10多个小时才弄明白,将来可能会有人需要这个。希望能有帮助

谢谢