为什么将BitmapSource保存到PixelFormat.Format48bppRgb的位图时颜色会发生变化? [英] Why color changes when save a BitmapSource to Bitmap for PixelFormat.Format48bppRgb?

查看:227
本文介绍了为什么将BitmapSource保存到PixelFormat.Format48bppRgb的位图时颜色会发生变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a .NEF image. I installed the codec, then use below code to show it:

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并将其保存:

    Then use below code to create the bmp and save it:

    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图像,第三个是图像控件中显示的图像.

    It saves the bmp file, however, it seems the color in the bmp has some changes. I attached 3 screen shots. The 1st one is the saved bmp file shown by Windows Photo Viewer, the 2nd one is the original .NEF image, the 3rd one is the image shown in the image control.

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

    We can see that they are all similar. However, the 2nd and 3rd ones have similar colors, and they are different from the 1st one.

    我搜索了很多东西,发现的所有东西都与我正在做的事情相似.但是,它们全部用于Format32bppRgb.问题可能是因为我使用的图像是Format48bppRgb吗?有人知道吗以及如何解决这个问题?

    I searched a lot and all I can find are similar to what I'm doing. However, they are all for Format32bppRgb. Could the issue is because the image I'm using is Format48bppRgb? Anyone have any idea? and how to fix this?

    谢谢

    推荐答案

    我认识到第一张和第二张图片之间的区别是:如果我们在第一张图片中切换红色部分和B部分的颜色,则得到第二张图片.因此,我将代码更改为:

    I realize the difference between 1st and 2nd images is: if we switch the color Red part and B part in the 1st image, then we get the 2nd image. So, I changed the code to:

    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);
    

    现在,结果是正确的.

    当PixelFormat为Format48bppRgb时,BitmapSource.CopyPixels中似乎有一个错误.它以BGR而不是RGB的顺序复制像素.

    It seems there is a bug in BitmapSource.CopyPixels when the PixelFormat is Format48bppRgb. It copies the pixels in the order of BGR instead of RGB.

    任何人都知道为什么我必须切换R& B部分?还有其他建议吗?

    Anyone understands why I have to switch R & B part? Any other suggestions?

    无论如何,它现在可以正常工作.我花了十多个小时才弄清楚这一点,将来其他人可能会需要它.希望对您有所帮助.

    Anyway, it works fine now. It took me more than 10 hours to figure this out, someone else may need this in the future. Hope it helps.

    谢谢

    这篇关于为什么将BitmapSource保存到PixelFormat.Format48bppRgb的位图时颜色会发生变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆