3

My .png image is stored on a Uri object and it has the following format

"pack://application:,,,/AppName.Modules.App.Shared;component/Images/AppName_logo.png"

How do I load this image onto a System.Drawing.Bitmap object?

  • look at other link http://stackoverflow.com/questions/3386749/loading-a-file-to-a-bitmap-but-leaving-the-original-file-intact – lordkain Aug 08 '13 at 17:27

1 Answers1

0

Assuming you are using WPF, you can first load the image as a BitmapImage and then convert it.

See this answer to "Converting BitmapImage to Bitmap and vice versa"

BitmapImage bi = new BitmapImage(
    new Uri("pack://application:,,,/AppName.Modules.App.Shared;component/Images/AppName_logo.png"));

Bitmap b = BitmapImage2Bitmap(bi);

private Bitmap BitmapImage2Bitmap(BitmapImage bitmapImage)
{
    using (MemoryStream outStream = new MemoryStream())
    {
        BitmapEncoder enc = new BmpBitmapEncoder();
        enc.Frames.Add(BitmapFrame.Create(bitmapImage));
        enc.Save(outStream);
        System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(outStream);

        return new Bitmap(bitmap);
    }
}
Scott Solmer
  • 3,871
  • 6
  • 44
  • 72
  • 1
    This is good, but if the original PNG contains transparent pixels, they seem to be converted to black pixels in the resulting Bitmap – p10ben Feb 02 '22 at 19:16