Create 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 Bitmap
  2. Create a New Bitmap
  3. Create Bitmap with Filled Rectangle
  4. Load an Image in Bitmap
  5. Resize an Existing Image
  6. Create a Bitmap from MemoryStream into a File

C# API to Create 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 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 Bitmap with Filled Rectangle

We can draw 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 create a new bitmap with a filled rectangle in C#.

Create-Bitmap-with-Filled-Rectangle

Create Bitmap with Filled Rectangle

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#

Resize Existing Image using 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#.

Resize-Existing-Image-using-CSharp

Resize Existing Image using C#

Create a Bitmap from MemoryStream into a File 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 save a bitmap file from MemoryStream 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 the documentation and explore various features supported by the API. In case of any ambiguity, please feel free to contact us on our forum.

See Also