What Will I Learn?
- You will learn how to create a bitmap in memory.
- You will learn how to change a bitmap compositing mode in C# using Visual Studio.
Requirements
- Visual Studio Code
Difficulty
- Intermediate
Tutorial: How to create a bitmap in memory and set compositing mode in C#
In this tutorial you will learn that how to create a bitmap in memory and how to change a bitmap compositing mode in C# using Visual Studio.
Create Bitmap:
First you have to create an object of bitmap class. Bitmap class constructor takes three parameters height, width and pixel format. Pixel format should be Format32bppArgb so that it will allow you to modify bitmap in memory. In memory means it will not direct modify on form. First it will change in memory then will display on form. So it is fast way to modify the bitmap.
Graphics:
Create two objects of graphics class one is from form and other is from bitmap file so that you will be able to modify the bitmap. To create a graphics object from file we have a function of graphics class named fromimage (). This function takes an object of image class and returns an object of graphics class.
Compositing Mode:
Compositing mode basically helps you to decide that how to draw the composited images on the form. There different types of compositing mode. To set the mode we have a property of graphics class names compositingMode. This property takes a values of enumeration named CompositingMode.
Draw Image:
After setting the property draw the image on form using function of graphics class named draw image. This function takes five parameters. Image class object, x, y coordinates of form where image will draw, height and width.
Now write the following code on Form Paint Event:
C#
private void OnPaint(object sender, PaintEventArgs e)
{
Graphics gForm = e.Graphics;
gForm.FillRectangle(Brushes.White, this.ClientRectangle);
for (int i = 1; i <= 7; ++i)
{
Rectangle r = new Rectangle(i * 40 - 15, 0, 15,this.ClientRectangle.Height);
gForm.FillRectangle(Brushes.Orange, r);
}
Bitmap bmp = new Bitmap(260, 260, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
Graphics gBmp = Graphics.FromImage(bmp);
gBmp.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;
gForm.DrawImage(bmp, 20, 20, bmp.Width, bmp.Height);
bmp.Dispose();
gBmp.Dispose();
}
VB
Private Sub OnPaint(ByVal sender As Object, ByVal e As PaintEventArgs)
Dim gForm As Graphics = e.Graphics
gForm.FillRectangle(Brushes.White, Me.ClientRectangle)
Dim i As Integer = 1
Do While (i <= 7)
Loop
i
Dim r As Rectangle = New Rectangle(((i * 40) _
- 15), 0, 15, Me.ClientRectangle.Height)
gForm.FillRectangle(Brushes.Orange, r)
Dim bmp As Bitmap = New Bitmap(260, 260, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
Dim gBmp As Graphics = Graphics.FromImage(bmp)
gBmp.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy
gForm.DrawImage(bmp, 20, 20, bmp.Width, bmp.Height)
bmp.Dispose
gBmp.Dispose
End Sub
This is simple code to create bitmap and set compositing mode.
Now write the following code on FORM LOAD event:
C#
private void Form1_Load(object sender, EventArgs e)
{
this.Text = "Utopian Application";
}
VB
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
Me.Text = "Utopian Application"
End Sub
This tutorial tells that how to create a bitmap in memory and how to change a bitmap compositing mode in C# using Visual Studio Code.
Posted on Utopian.io - Rewarding Open Source Contributors
Your contribution cannot be approved because it does not follow the Utopian Rules.
Related Rule:
Suggestions:
You can contact us on Discord.
[utopian-moderator]
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit