Software & Finance





C# - Displaying Images and Convering Type between jpeg, tiff, png, bmp, etc





C# provides an easy to way to load and display images on the screen very easily. You need to write only couple of lines of code to implement this. Besides changing the image type (like convert bmp to jpeg or tiff or png) is also very easy to implement.

System.Drawing.Image is the class you can use to load images and save it as another type.

To know the list of image files supported, loot at System.Drawing.Imaging.ImageFormat

 

Source Code


void LoadImage(Graphics g, string filename)

{

        Image MyImg = Image.FromFile(filename);

      g.DrawImage(MyImg,

              new RectangleF(

                    10,

                  10,

                  MyImg.Width / 2,

                  MyImg.Height / 2)

                );

 

MyImg.Save("c:\\r.gif", System.Drawing.Imaging.ImageFormat.Gif);

MyImg.Save("c:\\r.png", System.Drawing.Imaging.ImageFormat.Png);

MyImg.Save("c:\\r.tiff", System.Drawing.Imaging.ImageFormat.Tiff); 

}

 

Output


 

Displays the loaded image