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

前端 未结 2 1347
心在旅途
心在旅途 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:40

    Use Image.FromFile(String, Boolean) or Bitmap.FromFile(String, Boolean)

    And set the Boolean value to true. All image properties will be saved in the new image.

    Here String is the filename with full path...

    If the image is already loaded in a program and you want to create a new Bitmap with it, you can also use

    MemoryStream ms = new MemoryStream();
    img.Save(ms, ImageFormat.Bmp); // img is any Image, previously opened or came as a parameter
    Bitmap bmp = (Bitmap)Bitmap.FromStream(ms,true);
    

    Common alternative is

    Bitmap bmp = new Bitmap(img); // this won't preserve img.PixelFormat
    
    0 讨论(0)
  • 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!

    0 讨论(0)
提交回复
热议问题