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.
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.
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.
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}
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
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.
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"); } }
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.
Size
property on the Image type is another representation of the Height and Width values.System.Drawing
namespace to your C# project.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.
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.
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.
SaveAdd
method allows you to add extra frames to an existing image on the disk.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.