Home
Map
Image TypeUse the Image type from the System.Drawing namespace to get information about image files.
C#
This page was last reviewed on Sep 28, 2022.
Image. With this type in the C# language and .NET, you can act upon an abstraction of all those image types. Images are complex and there are many image formats.
Constructor. There is no constructor for the Image type. Instead, look into using the Image.FromFile or Image.FromStream methods. These will return a new instances of the Image type.
FromFile. The FromFile method is the easiest way to get an instance of a new Image in memory. Internally, FromFile uses functions from the GDI+ libraries of Microsoft Windows.
Tip The easiest way to use Image.FromFile is to pass it a single argument: the file name. Then, you can access the Image object itself.
using System; using System.Drawing; class Program { static void Main() { string[] array = { @"C:\thumbnail.png", @"C:\Users\Sam2\bluearrow1.png" }; foreach (string fileName in array) { Image image = Image.FromFile(fileName); int width = image.Width; int height = image.Height; Console.WriteLine("{0}: {1} {2}", fileName, width, height); } } }
C:\thumbnail.png: 9 40 C:\Users\Sam2\bluearrow1.png: 208 208
Image.FromStream. The Image.FromStream method works the same as the FromFile method but acts upon a Stream instance to get the image data.
And This means you can get an Image from a byte array in memory with a MemoryStream, or a file with FileStream.
using System; using System.Drawing; using System.IO; class Program { static void Main() { using (FileStream stream = File.Open("C:\\t4\\2Z", FileMode.Open)) using (Image image = Image.FromStream(stream)) { Console.WriteLine(image.Size); } } }
{Width=280, Height=248}
Tag. We can assign any object reference to the Tag property. The Tag here is much like the Tag property in Windows Forms—we can attach a reference of any type in memory.
using System; using System.Drawing; class Program { static void Main() { using (Image image = Image.FromFile("C:\\t4\\2Z")) { image.Tag = "Dot Net Perls image"; // Do stuff. Console.WriteLine(image.Tag); } } }
Dot Net Perls image
Resolution. Most bitmap images, such as PNG files and JPG files, will have a HorizontalResolution and VerticalResolution of 72 each.
using System; using System.Drawing; class Program { static void Main() { Image i = Image.FromFile("C:\\t4\\2Z"); float h = i.HorizontalResolution; float v = i.VerticalResolution; Console.WriteLine("{0}, {1}", h, v); } }
72, 72
RotateFlip. If you need to rotate or flip an image, the RotateFlip method is just what you need. The method allows you to both rotate and flip in a single method call.
Note The image used was on the front page of this site. It has text on it, so you can tell how the image was rotated and flipped.
using System; using System.Drawing; class Program { const string _input = "C:\\t4\\2Z"; static void Main() { M(RotateFlipType.Rotate180FlipNone); M(RotateFlipType.Rotate180FlipX); M(RotateFlipType.Rotate180FlipXY); M(RotateFlipType.Rotate180FlipY); M(RotateFlipType.Rotate270FlipNone); M(RotateFlipType.Rotate270FlipX); M(RotateFlipType.Rotate270FlipXY); M(RotateFlipType.Rotate270FlipY); M(RotateFlipType.Rotate90FlipNone); M(RotateFlipType.Rotate90FlipX); M(RotateFlipType.Rotate90FlipXY); M(RotateFlipType.Rotate90FlipY); M(RotateFlipType.RotateNoneFlipNone); M(RotateFlipType.RotateNoneFlipX); M(RotateFlipType.RotateNoneFlipXY); M(RotateFlipType.RotateNoneFlipY); } static void M(RotateFlipType type) { Image i = Image.FromFile(_input); i.RotateFlip(type); i.Save("C:\\" + type.ToString() + ".png"); } }
Height, Width. The Height and Width properties can be used to generate HTML files that contain the height and width attributes for an image that is on the disk.
Also The Size property on the Image type is another representation of the Height and Width values.
Start Place an image file (such as a PNG) somewhere on your computer. Then, add the System.Drawing namespace to your C# project.
Next Use the code here to open the Image (with Image.FromFile), and then access the Width, Height, or PhysicalDimension properties.
using System; using System.Drawing; class Program { static void Main() { Image image = Image.FromFile("C:\\background1.png", false); Console.WriteLine(image.Width); Console.WriteLine(image.Height); Console.WriteLine(image.PhysicalDimension); } }
180 110 {Width=180, Height=110}
GetThumbnailImage. To save bandwidth, websites often use thumbnail images that users can click on to see larger versions. GetThumbnailImage() can generate these images.
Dispose. Because the Image type implements IDisposable, it can be used with the using statement in a resource acquisition block. This can reduce resource usage.
using
Image formats. Obviously, not all image formats are supported by the Image type. The PNG type is supported. More exotic types such as Photoshop image formats would not be supported.
Save. After providing a target file name, the Save method will write the image data in memory to the location. This is useful after rotating or resizing an image.
Detail The SaveAdd method allows you to add extra frames to an existing image on the disk.
Detail The concept of frames is relevant for images (such as TIFF images) that support multiple pages or frames.
Note This concept is only relevant for some kinds of images, such as TIFF. A frame may represent a page in a multi-page image.
Summary. We explored many different aspects of the Image type in C#. With abstraction, the Image type allows us to use image formats in a simple way, but with limitations.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Sep 28, 2022 (edit).
Home
Changes
© 2007-2024 Sam Allen.