Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade

How to Create Graphics in Windows Using System.Drawing

System.Drawing is a namespace in .NET that provides access to basic graphics functionality. It is widely used for rendering images, drawing shapes, and manipulating graphical data. This namespace is particularly important for developers who need to create custom graphics, generate dynamic images, or perform image processing tasks in their applications.

In the context of a Windows environment, System.Drawing can be utilized through various .NET languages such as C#. It is essential for creating desktop applications that require graphical elements, such as charts, diagrams, or custom UI components. This article will guide you through the basics of using System.Drawing to create and manipulate graphics in a Windows environment.

Examples:

  1. Creating a Simple Bitmap Image:
using System;
using System.Drawing;

class Program
{
    static void Main()
    {
        // Create a new bitmap image of width 200 and height 100
        Bitmap bmp = new Bitmap(200, 100);

        // Create a graphics object from the bitmap
        Graphics g = Graphics.FromImage(bmp);

        // Fill the background with white color
        g.Clear(Color.White);

        // Draw a red rectangle
        g.DrawRectangle(Pens.Red, 10, 10, 180, 80);

        // Draw a blue ellipse
        g.DrawEllipse(Pens.Blue, 10, 10, 180, 80);

        // Save the bitmap to a file
        bmp.Save("example.bmp");

        // Dispose of the graphics object and bitmap
        g.Dispose();
        bmp.Dispose();

        Console.WriteLine("Image created successfully!");
    }
}

To compile and run this program, you can use the following commands in the Developer Command Prompt for Visual Studio:

csc /reference:System.Drawing.dll Program.cs
Program.exe
  1. Drawing Text on an Image:
using System;
using System.Drawing;

class Program
{
    static void Main()
    {
        // Create a new bitmap image
        Bitmap bmp = new Bitmap(300, 150);

        // Create a graphics object from the bitmap
        Graphics g = Graphics.FromImage(bmp);

        // Fill the background with white color
        g.Clear(Color.White);

        // Define the font and brush
        Font font = new Font("Arial", 20);
        Brush brush = Brushes.Black;

        // Draw the text
        g.DrawString("Hello, World!", font, brush, new PointF(10, 50));

        // Save the bitmap to a file
        bmp.Save("text_example.bmp");

        // Dispose of the graphics object and bitmap
        g.Dispose();
        bmp.Dispose();

        Console.WriteLine("Image with text created successfully!");
    }
}

To compile and run this program, use the same commands as mentioned above.

To share Download PDF

Gostou do artigo? Deixe sua avaliação!
Sua opinião é muito importante para nós. Clique em um dos botões abaixo para nos dizer o que achou deste conteúdo.