How can I get the resolution of an image? (JPEG, GIF, PNG, JPG)

后端 未结 3 720
醉梦人生
醉梦人生 2020-12-30 13:12

I found this method:

Graphics g = e.Graphics;
Bitmap bmp = new Bitmap(\"winter.jpg\");
g.DrawImage(bmp, 0, 0);
Console.WriteLine(\"Screen resolution: \" + g.         


        
3条回答
  •  别那么骄傲
    2020-12-30 13:19

    Surely you've answered your own question, it's as simple as this:

    Bitmap bmp = new Bitmap(@"winter.bmp");     
    Console.WriteLine("Image Width: " + bmp.Width);
    Console.WriteLine("Image Height: " + bmp.Height);
    

    The DPI info is really only useful when you are displaying or printing the image. In the sample code it is converting the image to a Graphics object that can be used to draw it to the screen.

    To answer the 2nd part of you question, you could parse the JPEG file header to find the width/height information without actually loading in the image. But to do this you'd need to know the header information and file format for JPEG files.

提交回复
热议问题