Quantcast
Channel: WPF Manipulating DPI of Images
Viewing all articles
Browse latest Browse all 2

WPF Manipulating DPI of Images

$
0
0
So I have a .NET 4.0 WPF applicaton that does some image manipulation. Things like scales the image if it is too large. One thing I wanted it to do was update the DPI of an image if it was not 96 x 96. Below is the basic code that I was using, hard coded to a specific input and output image. It works great on almost all images that I update the DPI of, but it does not work on a few images for some reason. http://i48.tinypic.com/34sff5s.png is an example of the image that doesn't work. The DPI of the output image is something like 95.98... (you may have to look at the output image in something like GIMP to get the true DPI, windows explorer file details rounds the DPI to 96).

Any ideas on what I might be doing wrong? Am I in trouble because I tried using the WPF image objects instead of the old image objects?

   
 private static void ResizeAndOpenImage()
        {            
            using (MemoryStream ms = new MemoryStream())
            {
                using (FileStream fs = File.OpenRead(@"C:\Test\Images\34sff5s.png"))
                {
                    //get the decoder for this extension                
                    PngBitmapDecoder decoder = new PngBitmapDecoder(fs, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
                    if (decoder != null)
                    {
                        BitmapSource sourceToResize = decoder.Frames.First();
                        //check if the DPI is less than 96, if so, resize it to 96
                        if (sourceToResize.DpiX != 96 || sourceToResize.DpiY != 96)
                        {
                            double dpi = 96;
                            int width = sourceToResize.PixelWidth;
                            int height = sourceToResize.PixelHeight;

                            int stride = width * 4; // 4 bytes per pixel
                            byte[] pixelData = new byte[stride * height];
                            sourceToResize.CopyPixels(pixelData, stride, 0);

                            sourceToResize = BitmapSource.Create(width, height, dpi, dpi, sourceToResize.Format, null, pixelData, stride);

                            using (FileStream destinationStream = new FileStream(@"C:\Test\Results\34sff5s.png", FileMode.Create))
                            {
                                PngBitmapEncoder encoder = new PngBitmapEncoder();
                                encoder.Frames.Add(BitmapFrame.Create(sourceToResize));
                                encoder.Save(destinationStream);                                
                            }                      
                        }
                    }
                }
            }            
        }


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images