Question

Does .NET support PixelFormat conversions? I can't find any methods or constructors to do what I want.

I am allowing the user to choose any image file and I am creating a new Bitmap class from the file.

Then I am drawing the image using custom painting. It's very slow and I am told that the GDI is much faster dealing with images in the 32bppPArgb pixel format.

How do I perform the conversion? Assuming I have a string for the filename which could be a JPEG, TIF, PNG, etc., and I want to load it to a Bitmap class.

Edit:

Here is what I am trying to do, but it's not loading the image properly. The image is not filling the entire bitmap when I draw it.

using (Bitmap tempImage = new Bitmap(filename))
{
    Image = new Bitmap(tempImage.Width, tempImage.Height, PixelFormat.Format32bppPArgb);

    using (Graphics g = Graphics.FromImage(Image))
    {
        g.DrawImageUnscaled(tempImage, 0, 0);
    }
}
Was it helpful?

Solution

If you have a scaling issue it is not caused by the pixel format. Instead, it is likely caused by DPI settings in the source image file. Try this instead of .DrawImageUnscaled:

  g.DrawImage(
      tempImage,
      new Reactangle( Point.Empty, Image.Size ),
      new Reactangle( Point.Empty, Image.Size ),
      GraphicsUnit.Pixels );

That's what I always used instead of .DrawImageUnscaled - it has failed me too many times!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top