What is the usage of bitmap in C#?
In C#, the Bitmap class is used for image processing. It offers various methods and properties for creating, editing, and manipulating images. Here are some common uses of the Bitmap class:
- Create a Bitmap object: You can create a Bitmap object using the constructor of the Bitmap class. For example, you can create a new Bitmap object by specifying the path of an image file or by specifying a specific width and height.
Bitmap bitmap1 = new Bitmap("image.jpg");
Bitmap bitmap2 = new Bitmap(800, 600);
- Accessing and modifying pixels: The GetPixel and SetPixel methods can be used to access and modify the pixels of an image. The GetPixel method is used to retrieve the color of a pixel at a specific position, while the SetPixel method is used to change the color of a pixel at a specific position.
Color color = bitmap1.GetPixel(100, 100);
bitmap2.SetPixel(200, 200, Color.Red);
- Create an image: You can use the DrawImage method of the Graphics class to draw a Bitmap object onto another image.
Graphics graphics = Graphics.FromImage(bitmap2);
graphics.DrawImage(bitmap1, new Point(0, 0));
- Resize and Adjust Size: You can use the SetResolution method of the Bitmap class to set the resolution of the image, and use the DrawImage method of the Graphics class to scale the image to the desired size.
bitmap1.SetResolution(300, 300);
graphics.DrawImage(bitmap1, new Rectangle(0, 0, 400, 300));
- Saving and loading images: You can use the Save method to save a Bitmap object as an image file, and use the FromFile method to load a Bitmap object from a file.
bitmap1.Save("newimage.jpg");
Bitmap bitmap3 = Bitmap.FromFile("image.jpg") as Bitmap;
The above are some common uses of the Bitmap class that can help you create, edit, and manipulate images.