Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use C # memory Graphics object

2025-04-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article focuses on "how to use C # memory Graphics objects", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to use C # memory Graphics objects"!

SetBackgroundBitmap function first saves the background image of the form to the BackgroundBitmap variable, and then according to the bitmap image outline and transparent color to create Region,BitmapToRegion is used to complete the conversion from Bitmap to Region, the program then pays the value of the Region to the Region property of the form to complete the creation of the irregular form.

Public void SetBackgroundBitmap (Image image, Color transparencyColor) {BackgroundBitmap = new Bitmap (image); Width = BackgroundBitmap.Width; Height = BackgroundBitmap.Height; Region = BitmapToRegion (BackgroundBitmap, transparencyColor);} public Region BitmapToRegion (Bitmap bitmap, Color transparencyColor) {if (bitmap = = null) throw new ArgumentNullException ("Bitmap", "Bitmap cannot be null!"); int height = bitmap.Height; int width = bitmap.Width; GraphicsPath path = new GraphicsPath (); for (int j = 0; j < height) If (bitmap.GetPixel (I, j) = = transparencyColor) continue; int x0 = I; while ((I < width) & & (bitmap.GetPixel (I, j)! = transparencyColor)) istories; path.AddRectangle (new Rectangle (x0, j, I-x0,1));} Region region = new Region (path); path.Dispose (); return region;}

Notice that the background of the form and the drawing of the text are done in the overloaded OnPaintBackground method, and the double buffer technology is used for the drawing operation, as follows:

Protected override void OnPaintBackground (PaintEventArgs e)

{

Graphics grfx = e.Graphics

Grfx.PageUnit = GraphicsUnit.Pixel

Graphics offScreenGraphics

Bitmap offscreenBitmap

OffscreenBitmap = new Bitmap (BackgroundBitmap.Width, BackgroundBitmap.Height)

OffScreenGraphics = Graphics.FromImage (offscreenBitmap)

If (BackgroundBitmap! = null)

{

OffScreenGraphics.DrawImage (BackgroundBitmap, 0,0

BackgroundBitmap.Width, BackgroundBitmap.Height)

}

DrawText (offScreenGraphics)

Grfx.DrawImage (offscreenBitmap, 0,0)

}

The above code first returns the Graphics of the drawing surface of the form and saves it in the variable grfx, then creates a C# memory Graphics object offScreenGraphics and a memory bitmap object offscreenBitmap, and pays the reference of the memory bitmap object to offScreenGraphics, so that all drawing operations on offScreenGraphics also act on offscreenBitmap, and then the background image BackgroundBitmap that needs to be drawn to the surface of the notification form is drawn to the C# memory Graphics object. The DrawText function calls Graphics.DrawString to display the text in a specific area of the form as needed. * call Graphics.DrawImage to display the image that has been drawn in memory to the surface of the notification form.

We also need to capture the mouse operation of the form, and there are three operations to do here

1. Handle dragging form operations

2. Handle the closing operation of the notification form

3. Click on the content area.

All three operations need to detect the relationship between the current position of the mouse and the inclusion of each Rectangle region. As long as we click to land in a specific area, we will deal with it accordingly, as follows:

Private void TaskbarForm_MouseDown (object sender, MouseEventArgs e) {if (e.Button = = MouseButtons.Left) {if (TitlebarRectangle.Contains (e.Location)) / / drag {ReleaseCapture () when clicking the title bar; / / release mouse capture SendMessage (Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0) / / send the left-clicked message to the form (title bar)} if (CloseBtnRectangle.Contains (e.Location)) / / Click the Close button to close {this.Hide (); currentTop = 1;} if (ContentRectangle.Contains (e.Location)) / / Click the content area {System.Diagnostics.Process.Start ("http://www.Rithia.com");})

The program can well display, stay and hide the notification form, and has a simple skin-changing mechanism. After using the double buffer drawing technology, it can ensure that the drawing of the form is smooth and does not flicker.

At this point, I believe you have a deeper understanding of "how to use C # memory Graphics objects". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report