Learn how to create a bitmap programmatically in C# : https://blog.aspose.com/create-bitmap-in-csharp/
The following topics are covered in this article:
Learn how to create a bitmap programmatically in C# : https://blog.aspose.com/create-bitmap-in-csharp/
The following topics are covered in this article:
// 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"); |