Create, Load, Fill, and Draw Bitmap in C#

A bitmap is used to create and store computer graphics. Bitmap images are widely used on web pages and digital devices such as cameras, smartphones, etc. The most common bitmap graphic file types are BMP, GIF, PNG, and JPEG. In this article, we will learn how to create a bitmap in C#.

The following topics shall be covered in this article:

  1. C# API to Create, Load, Fill, and Draw Bitmap
  2. Create a New Bitmap From Scratch
  3. Create a Bitmap from Byte Array
  4. Save Bitmap as Image File
  5. Draw Shapes on Bitmap
  6. Draw Text on Bitmap Image
  7. Fill a Bitmap with Solid Color
  8. Load an Image in Bitmap
  9. Resize an Existing Image using Bitmap

C# API to Create, Load, Fill, and Draw Bitmap - Free Download

To create a bitmap, we will be using the Aspose.Drawing for .NET API. It is a cross-platform 2D graphics library for drawing text, geometries, and images programmatically. It allows loading, saving, and manipulating the supported file formats.

Please either download the DLL of the API or install it using NuGet.

PM> Install-Package Aspose.Drawing

Create a New Bitmap in C#

We can create a new bitmap from scratch by following the steps given below:

  1. Firstly, create a new bitmap using the Bitmap class with the specified size and format.
  2. Next, create a new Graphics object from the Bitmap object using the FromImage() method.
  3. Then, initialize a Pen class object with the specified color and size.
  4. After that, call the DrawEllipse() method to draw an Ellipse with a specified size.
  5. Finally, save the output image using the Save() method.

The following code sample shows how to create a new bitmap in C#.

Create-a-New-Bitmap-in-CSharp

Create a new Bitmap in C#

Create a Bitmap from Byte Array in C#

We can create a bitmap from memory stream bytes by following the steps given below:

  1. Read image file into a byte array.
  2. Create a new instance of the MemoryStream using the byte array object.
  3. Create a new bitmap using the Bitmap class with the MemoryStream object.
  4. Finally, save the image using the Save() method.

The following code sample shows how to create a bitmap from Byte Array using MemoryStream in C#.

C# Save Bitmap to a File

We can save bitmap image to a file in various supported file formats by following the steps mentioned earlier. However, we just need to mention the desired save file format extension while saving the image file using the Save(string filename, ImageFormat format) method in the last step.

  • Save a Bitmap as a BMP File
bitmap.Save(@"C:\Files\output.bmp", ImageFormat.Bmp);
  • Save a Bitmap as a PNG File
bitmap.Save(@"C:\Files\output.png", ImageFormat.Png);
  • Save a Bitmap as a JPG File
bitmap.Save(@"C:\Files\output.jpg", ImageFormat.Jpeg);
  • Save a Bitmap as a GIF File
bitmap.Save(@"C:\Files\output.gif", ImageFormat.Gif);

C# Draw on Bitmap

We can draw various shapes and images on a bitmap by following the steps mentioned earlier. However, we just need to use the shape specific methods to draw the desired shapes.

We can draw the following shapes using the provided specific methods to draw on bitmap:

  • Arc: We can draw an Arc using the DrawArc() method.
  • Bezier: We can draw a Bezier using the DrawBezier() method.
  • A series of Bézier splines: We can draw a series of Bezier splines using the DrawBeziers() method.
  • Curve: We can draw a Curved shape using the DrawCurve() method.
  • ClosedCurve: We can draw a closed curve using the DrawClosedCurve() method.
  • Ellipse: We can draw an Ellipse using the DrawEllipse() method.
  • Line: We can draw a Line using the DrawLine() method.
  • A series of Lines: We can draw a series of Lines using the DrawLines() method.
  • Path: We can draw a Path using the DrawPath() method.
  • Pie: We can draw a Pie using the DrawPie() method.
  • Polygon: We can draw a Polygon using the DrawPolygon() method.
  • Rectangle: We can draw a Rectangle using the DrawRectangle() method.
  • A series of Rectangles: We can draw a series of Rectangles using the DrawRectangles() method.

We can draw the images on the bitmap using the following methods:

  • Icon: We can draw the image represented by the specified Icon within the area specified by a Rectangle structure using the DrawIcon() method.
  • Image: We can draw any specified Image using the DrawImage() method.

C# Draw Text on Bitmap

We can draw any string on a bitmap by following the steps given below:

  1. Firstly, create a new bitmap using the Bitmap class with the specified size.
  2. Next, create a new Graphics object from the Bitmap object using the FromImage() method.
  3. Then, define a Font class object with the desired font family, style and size.
  4. Meanwhile, initialize a SolidBrush object with the specified color.
  5. After that, call the DrawString() method with the text to show, Font, and Brush objects as arguments.
  6. Finally, save the output image using the Save() method.

The following code sample shows how to draw text on a bitmap in C#.

C# Draw Text on Bitmap

C# Draw Text on Bitmap

Fill a Bitmap with Solid Color in C#

We can fill a bitmap with a solid color using a filled rectangle and save it in a new bitmap by following the steps given below:

  1. Firstly, create a new bitmap using the Bitmap class with the specified size.
  2. Next, create a new Graphics object from the Bitmap object using the FromImage() method.
  3. Then, initialize a Rectangle class object with the specified location and size.
  4. After that, call the FillRectangle() method with brush color and the Rectangle.
  5. Finally, save the output image using the Save() method.

The following code sample shows how to fill a bitmap with a solid color using a rectangle in C#.

C# Draw on Bitmap and Fill with Rectangle

C# Draw Rectangle on Bitmap and Fill with a Color

Load an Image in Bitmap using C#

We can also load an existing image in a bitmap and save it as a new bitmap by following the steps given below:

  1. Firstly, load an image using the Bitmap class.
  2. Next, create a new bitmap using the Bitmap class with the specified size.
  3. Then, create a new Graphics object from the newly created Bitmap object using the FromImage() method.
  4. After that, call the DrawImage() method with the image and location coordinates as arguments.
  5. Finally, save the output image using the Save() method.

The following code sample shows how to load an image in a bitmap using C#.

Load-an-Image-in-Bitmap-using-CSharp

Load an Image in Bitmap using C#

How to Resize Image using Bitmap in C#

We can load and resize an existing image in a bitmap and save it as a new bitmap by following the steps given below:

  1. Load an existing image using the Bitmap class.
  2. Create a new bitmap using the Bitmap class with the loaded image and specified size.
  3. Finally, save the output image using the Save() method.

The following code sample shows how to resize an existing image and save it as a new bitmap in C#.

How to Resize Image using Bitmap in C#

Resize Existing Image using C#

Get Free Temporary License

You can get a free temporary license to try Aspose.Drawing for .NET without evaluation limitations.

Conclusion

In this article, we have learned how to:

  • create a new bitmap;
  • draw rectangle, ellipse, etc. on a bitmap;
  • load an existing image;
  • resize an existing image and create a new bitmap with a new size;
  • save a Bitmap image from MemoryStream or byte array into a file;
  • set a width and height of a bitmap in C#.

Besides creating a new bitmap in C#, you can learn more about Aspose.Drawing for .NET using documentation and explore various features supported by the API. In case of any ambiguity, please feel free to contact us on our free support forum.

See Also