Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active December 16, 2022 13:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/39d15c17ffd466207023babc8401d4b9 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/39d15c17ffd466207023babc8401d4b9 to your computer and use it in GitHub Desktop.
Create Bitmap Programmatically in C#
// This code example demonstrates how to create a new bitmap programmatically in C#.
// Initialize Bitmap object
Bitmap bitmap = new Bitmap(1000, 800, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
// Create a new Graphics
Graphics graphics = Graphics.FromImage(bitmap);
// Initialize a pen
Pen pen = new Pen(Color.Red, 5);
// Draw a rectangle
graphics.DrawEllipse(pen, new Rectangle(0, 0, 700, 700));
// Save the file
bitmap.Save(@"C:\Files\Drawings\output.png");
// This code example demonstrates how to draw a text on a new bitmap programmatically in C#.
// Initialize Bitmap object
Bitmap bitmap = new Bitmap(500,200);
// Create a new Graphics
Graphics graphics = Graphics.FromImage(bitmap);
// Initialize a Font
var font = new Font(FontFamily.GenericSerif, 40f, FontStyle.Bold);
// Initialize a Brush
var brush = new SolidBrush(Color.Red);
// Draw a rectangle
graphics.DrawString("Welcome to Bitmap!", font, brush, 10, 20);
// Save the file
bitmap.Save(@"C:\Files\Drawings\output.png", ImageFormat.Png);
// This code example demonstrates how to create a new bitmap from memory stream bytes in C#.
// Load the image and read all the bytes
var file = File.ReadAllBytes(@"C:\Files\Drawings\sample.jpg");
// Create memory stream
MemoryStream stream = new MemoryStream(file);
// Create a new bitmap
Bitmap bitmap = new Bitmap(stream);
// Save the file
bitmap.Save(@"C:\Files\Drawings\output.jpg");
// This code example demonstrates how to load an image into a bitmap and save as a new bitmap.
// Load the image:
Bitmap image = new Bitmap(@"C:\Files\Drawings\sample.jpg");
// Initialize Bitmap with spefcified size
Bitmap bitmap = new Bitmap(1000, 1000);
// Create a new Graphics
Graphics graphics = Graphics.FromImage(bitmap);
// Draw image at specified location
graphics.DrawImage(image, 10, 10);
// Save the file
bitmap.Save(@"C:\Files\Drawings\output.png");
// This code example demonstrates how to create resize an existing image and create a new bitmap in C#.
// Load the image:
Bitmap image = new Bitmap(@"C:\Files\Drawings\sample.jpg");
// Create a new bitmap half of the size of loaded image:
Bitmap bitmap = new Bitmap(image, (int)(image.Width * 0.5), (int)(image.Height * 0.5));
// Save the file
bitmap.Save(@"C:\Files\Drawings\output.jpg");
// This code example demonstrates how to create a new bitmap with filled rectangle in C#.
// Initialize Bitmap with spefcified size
Bitmap bitmap = new Bitmap(500, 500);
// Create a new Graphics
Graphics graph = Graphics.FromImage(bitmap);
// Define a rectangle
Rectangle ImageSize = new Rectangle(0, 0, 500, 500);
// Draw a filled rectangle
graph.FillRectangle(Brushes.Gray, ImageSize);
// Save the file
bitmap.Save(@"C:\Files\Drawings\output.png");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment