C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
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.
C# program that uses Image.FromFile
using System;
using System.Drawing;
class Program
{
static void Main()
{
string[] array =
{
@"C:\thumbnail.png",
@"C:\Users\Sam2\Documents\Codex\main\images\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);
}
}
}
Output
C:\thumbnail.png: 9 40
C:\Users\Sam2\Documents\Codex\main\images\bluearrow1.png: 208 208
Tip: Because it goes into native code and external libraries, it is probably faster to use caching to reduce calls to Image.FromFile.
C# program that uses Image.FromStream method
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);
}
}
}
Output
{Width=280, Height=248}
C# program that uses Tag property
using System;
using System.Drawing;
class Program
{
static void Main()
{
using (Image image = Image.FromFile("C:\\t4\\2Z"))
{
image.Tag = "The Dev Codes image";
// Do stuff.
Console.WriteLine(image.Tag);
}
}
}
Output
The Dev Codes image
C# program that accesses resolution
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);
}
}
Output
72, 72
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.
C# program that flips image with RotateFlip
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");
}
}
Tip: The GetThumbnailImage method provides more information and a scaling algorithm for thumbnail images.
Thumbnail Image With GetThumbnailImageAlso: The Size property on the Image type is another representation of the Height and Width values.
Next: Use the code here to open the Image (with Image.FromFile), and then access the Width, Height, or PhysicalDimension properties.
C# program that gets image width and height
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);
}
}
Output
180
110
{Width=180, Height=110}
And: The best part is that you do not need to do anything directly with the bytes of the file.
Frames: Many of the methods available on the Image type refer to the concept of frames.
Tip: This concept is only relevant for some kinds of images, such as TIFF. A frame may represent a page in a multi-page image.