Last active
May 22, 2026 07:11
-
-
Save aspose-com-gists/b08f9c9d7a3a16297a0d1af95f519f32 to your computer and use it in GitHub Desktop.
How to Create and Draw on Bitmaps with Aspose.Drawing in .NET
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // How to Create and Draw on Bitmaps with Aspose.Drawing in .NET | |
| // Read the full guide here: https://blog.aspose.com/drawing/how-to-create-and-draw-on-bitmaps-with-asposedrawing-in-dotnet/ | |
| using System; | |
| using Aspose.Drawing; | |
| using Aspose.Drawing.Imaging; | |
| using Aspose.Drawing.Imaging.ImageOptions; | |
| using Aspose.Drawing.Drawing2D; | |
| namespace BitmapDemo | |
| { | |
| class Program | |
| { | |
| static void Main() | |
| { | |
| // 1. Create a new bitmap with 800x600 size and 32‑bit ARGB pixel format | |
| using (Bitmap bitmap = new Bitmap(800, 600, PixelFormat.Format32bppArgb)) | |
| { | |
| // 2. Get a graphics surface for drawing | |
| using (Graphics graphics = bitmap.GetGraphics()) | |
| { | |
| // Clear background with white color | |
| graphics.Clear(Color.White); | |
| // 3. Draw a blue rectangle | |
| using (Pen bluePen = new Pen(Color.Blue, 5)) | |
| { | |
| graphics.DrawRectangle(bluePen, new Rectangle(100, 100, 600, 400)); | |
| } | |
| // 4. Draw a filled red ellipse | |
| using (Brush redBrush = new SolidBrush(Color.Red)) | |
| { | |
| graphics.FillEllipse(redBrush, new Rectangle(200, 150, 400, 300)); | |
| } | |
| // 5. Draw centered text | |
| using (Font font = new Font("Arial", 36, FontStyle.Bold)) | |
| using (Brush blackBrush = new SolidBrush(Color.Black)) | |
| { | |
| string text = "Aspose.Drawing Demo"; | |
| SizeF textSize = graphics.MeasureString(text, font); | |
| PointF location = new PointF( | |
| (bitmap.Width - textSize.Width) / 2, | |
| (bitmap.Height - textSize.Height) / 2); | |
| graphics.DrawString(text, font, blackBrush, location); | |
| } | |
| } | |
| // 6. Save the bitmap as PNG | |
| bitmap.Save("DemoOutput.png", ImageFormat.Png); | |
| } | |
| Console.WriteLine("Bitmap created and saved as DemoOutput.png"); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment