如何使用PaintEventArgs argumnt调用函数?
给出MSDN中的以下代码示例:
private void GetPixel_Example(PaintEventArgs e) { // Create a Bitmap object from an image file. Bitmap myBitmap = new Bitmap(@"C:\Users\tanyalebershtein\Desktop\Sample Pictures\Tulips.jpg"); // Get the color of a pixel within myBitmap. Color pixelColor = myBitmap.GetPixel(50, 50); // Fill a rectangle with pixelColor. SolidBrush pixelBrush = new SolidBrush(pixelColor); e.Graphics.FillRectangle(pixelBrush, 0, 0, 100, 100); }
如何调用Paint
函数?
从油漆事件这样的事情
private PictureBox pictureBox1 = new PictureBox(); pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint); private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { GetPixel_Example(e) ; }
您可以从表单的PaintEvent调用此方法,例如
public class Form1 : Form { public Form1() { this.Paint += new PaintEventHandler(Form1_Paint); } //.... private void Form1_Paint(object sender, PaintEventArgs e) { GetPixel_Example(e); } private void GetPixel_Example(PaintEventArgs e) { // Create a Bitmap object from an image file. Bitmap myBitmap = new Bitmap(@"C:\Users\tanyalebershtein\Desktop\Sample Pictures\Tulips.jpg"); // Get the color of a pixel within myBitmap. Color pixelColor = myBitmap.GetPixel(50, 50); // Fill a rectangle with pixelColor. SolidBrush pixelBrush = new SolidBrush(pixelColor); e.Graphics.FillRectangle(pixelBrush, 0, 0, 100, 100); } }
- 加载图像
- 在x = 50,y = 50时获取像素的颜色
- 在表单的Graphics -Object上绘制一个填充矩形,其颜色为0,0,大小为100,100
你不应该自己调用Paint
方法。 只要需要绘制组件,.NET框架就会调用Paint
方法。 这通常发生在窗口移动,最小化等时。
如果要告诉.NET框架重新绘制组件,请在组件或其父组件上调用Refresh()
。
- 如何在multithreading应用程序中安全地使用SmtpClient.SendAsync
- 取消HttpClient请求 – 为什么TaskCanceledException.CancellationToken.IsCancellationRequested为false?
- 用于为COM DLL创建.NET包装器的工具?
- C#建立从笔记本电脑内部蓝牙4.0到蓝牙低功耗(BLE)外设的流
- 使用c#中的身份validation复制文件
- C#/。NET – WinForms – 实例化一个表单而不显示它
- 远程连接到SQL Server 2005实例时出现C#exception
- 将SOAP标头添加到ASMX服务请求
- 无法加载文件或程序集Microsoft.ServiceModel.DomainServices.Hosting