Reading/preserving a PixelFormat.Format48bppRgb PNG Bitmap in .NET?

前端 未结 2 1346
心在旅途
心在旅途 2021-01-22 22:57

I\'ve been able to create a Format48bppRgb .PNG file (from some internal HDR data) using the the following C# code:

Bitmap bmp16 = new Bitmap(_viewer.Width, _vie         


        
2条回答
  •  礼貌的吻别
    2021-01-22 23:52

    FYI - I did find a .NET solution to this using System.Windows.Media.Imaging (I had been using strictly WinForms/GDI+ - this requires adding WPF assemblies, but works.) With this, I get a Format64bppArgb PixelFormat, so no lost information:

    using System.Windows.Media.Imaging; // Add PresentationCore, WindowsBase, System.Xaml
    ...
    
        // Open a Stream and decode a PNG image
    Stream imageStreamSource = new FileStream(fd.FileName, FileMode.Open, FileAccess.Read, FileShare.Read);
    PngBitmapDecoder decoder = new PngBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
    BitmapSource bitmapSource = decoder.Frames[0];
    
        // Convert WPF BitmapSource to GDI+ Bitmap
    Bitmap bmp = _bitmapFromSource(bitmapSource);
    String info = String.Format("PixelFormat: {0}", bmp.PixelFormat );
    MessageBox.Show(info);
    

    ...

    And this code snippet from: http://www.generoso.info/blog/wpf-system.drawing.bitmap-to-bitmapsource-and-viceversa.html

    private System.Drawing.Bitmap _bitmapFromSource(BitmapSource bitmapsource) 
    { 
        System.Drawing.Bitmap bitmap; 
        using (MemoryStream outStream = new MemoryStream()) 
        { 
            // from System.Media.BitmapImage to System.Drawing.Bitmap 
            BitmapEncoder enc = new BmpBitmapEncoder(); 
            enc.Frames.Add(BitmapFrame.Create(bitmapsource)); 
            enc.Save(outStream); 
            bitmap = new System.Drawing.Bitmap(outStream); 
        } 
        return bitmap; 
    } 
    

    If anyone has knows of a way to do this that doesn't require WPF, please share!

提交回复
热议问题