Image Class
Represents an image. This is an abstract class, so you typically use properties and methods of derived classes, such as the Bitmap class, to manipulate images.
Namespace: System.Drawing
Assembly: System.Drawing.dll
Syntax
public abstract class Image : MarshalByRefObject, ICloneable, IDisposable
Inheritance
System.Object
System.MarshalByRefObject
System.Drawing.Image
Derived Classes
Remarks
The Image
class is the base class for image objects. It provides a common set of methods and properties for working with various image formats, such as BMP, GIF, JPEG, PNG, and TIFF. You can load an image from a file or a stream, get its dimensions, save it in a different format, and perform basic image manipulation operations.
Important
The Image
class is abstract, meaning you cannot create an instance of Image
directly. You must use one of its derived classes, most commonly Bitmap
, to create and manipulate image objects.
Constructors
The Image
class has no public constructors, as it is an abstract class.
Methods
Name | Description |
---|---|
Clone() |
Creates an exact copy of this Image object. |
Dispose() |
Releases all resources used by this Image object. |
FromFile(string filename) |
Creates an Image from the specified file. (Static method, typically used with Bitmap ) |
FromStream(Stream stream) |
Creates an Image from the specified stream. (Static method, typically used with Bitmap ) |
GetPixel(int x, int y) |
Gets the color of the pixel at the specified coordinates. (Implemented in derived classes) |
Save(string filename) |
Saves this image to the specified file. |
Save(Stream stream, ImageFormat format) |
Saves this image to the specified stream in the specified format. |
Properties
Name | Description |
---|---|
BitsPerPixel |
Gets the number of bits per pixel for this Image . |
Bounds |
Gets a rectangle that represents the dimensions of this Image . |
Flags |
Gets flags that provide information about this Image . |
FrameDimensions |
Gets the dimensions of this Image . |
Height |
Gets the height of this Image in pixels. |
HorizontalResolution |
Gets the horizontal resolution, in pixels per inch, of this Image . |
ImageFormat |
Gets an ImageFormat object that contains information about the pixel format of this Image . |
Palette |
Gets or sets the palette of this Image . |
PixelFormat |
Gets the pixel format of this Image . |
PropertyItems |
Gets all the property items embedded in this Image . |
RawFormat |
Gets the raw format of this Image . |
Tag |
Gets or sets a user-defined object that contains data about this object. |
Width |
Gets the width of this Image in pixels. |
VerticalResolution |
Gets the vertical resolution, in pixels per inch, of this Image . |
Example Usage
Loading and displaying an image using the Bitmap
derived class:
using System.Drawing;
using System.Windows.Forms;
public partial class MyForm : Form
{
private PictureBox pictureBox1;
public MyForm()
{
InitializeComponent();
LoadImage("path/to/your/image.jpg");
}
private void LoadImage(string filePath)
{
try
{
// Using Bitmap, a derived class of Image
using (Image img = Image.FromFile(filePath))
{
pictureBox1.Image = img;
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom; // Or other desired mode
}
}
catch (Exception ex)
{
MessageBox.Show($"Error loading image: {ex.Message}");
}
}
private void InitializeComponent()
{
this.pictureBox1 = new PictureBox();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout();
// pictureBox1
this.pictureBox1.Dock = DockStyle.Fill;
this.pictureBox1.Location = new Point(0, 0);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new Size(800, 600);
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
// MyForm
this.ClientSize = new Size(800, 600);
this.Controls.Add(this.pictureBox1);
this.Name = "MyForm";
this.Text = "Image Display";
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.ResumeLayout(false);
}
}