C# 如何处理forms paint事件

C# 如何处理forms paint事件,c#,winforms,gdi+,C#,Winforms,Gdi+,我已经创建了一个表单,并希望使用以下代码示例: 带代码的说明为: 要运行此示例,请将其粘贴到表单中,并通过调用LockUnlockBitsExample方法处理表单的绘制事件,并将e作为PaintEventArgs传递 我已经将代码粘贴到表单中,但我不知道如何处理表单的绘制事件 namespace Laser_Control2 { public partial class LaserControlForm : Form { public Lase

我已经创建了一个表单,并希望使用以下代码示例:

带代码的说明为:

要运行此示例,请将其粘贴到表单中,并通过调用LockUnlockBitsExample方法处理表单的绘制事件,并将e作为PaintEventArgs传递

我已经将代码粘贴到表单中,但我不知道如何处理表单的绘制事件

    namespace Laser_Control2
    {
    public partial class LaserControlForm : Form
    {
        public LaserControlForm()
        {
            InitializeComponent();
        }

        // Manipulate a bitmap image
        // Extract the pixel map data
        // To run this example, paste it into a form and handle the form's Paint event
        //  by calling the LockUnlockBitsExample method, passing e as PaintEventArgs.
        private void LockUnlockBitsExample(PaintEventArgs e)
        {

            // Create a new bitmap.
            Bitmap bmp = new Bitmap("c:\\fakePhoto.jpg");

            // Lock the bitmap's bits.  
            Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
            System.Drawing.Imaging.BitmapData bmpData =
                bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
                bmp.PixelFormat);

            // Get the address of the first line.
            IntPtr ptr = bmpData.Scan0;

            // Declare an array to hold the bytes of the bitmap.
            int bytes = Math.Abs(bmpData.Stride) * bmp.Height;
            byte[] rgbValues = new byte[bytes];

            // Copy the RGB values into the array.
            System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);

            // Set every third value to 255. A 24bpp bitmap will look red.  
            for (int counter = 2; counter < rgbValues.Length; counter += 3)
                rgbValues[counter] = 255;

            // Copy the RGB values back to the bitmap
            System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);

            // Unlock the bits.
            bmp.UnlockBits(bmpData);

            // Draw the modified image.
            e.Graphics.DrawImage(bmp, 0, 150);

        }
        private void loadPdfButton_Click(object sender, EventArgs e)
        {
            if(openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                PdfDocument doc = new PdfDocument();                                                // Create Spire object - a new document
                doc.LoadFromFile(@"E:/Test/test2.pdf");                                             // Load test2.pdf into new document
                Image bmp = doc.SaveAsImage(0, 0, 32, 32);                                          // Save the .pdf doc as a .bmp

                pictureBox1.Image = bmp;
                bmp.Save("convertToBmp.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
                System.Diagnostics.Process.Start("convertToBmp.bmp");

            }
        }

        private void clearButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}
名称空间激光控制2
{
公共部分类激光控制器窗体:窗体
{
公共激光控制器()
{
初始化组件();
}
//操作位图图像
//提取像素地图数据
//要运行此示例,请将其粘贴到窗体中并处理窗体的绘制事件
//通过调用LockUnlockBitsSample方法,将e作为PaintEventArgs传递。
私有无效锁解锁位示例(PaintEventArgs e)
{
//创建一个新位图。
位图bmp=新位图(“c:\\fakePhoto.jpg”);
//锁定位图的位。
矩形rect=新矩形(0,0,bmp.Width,bmp.Height);
System.Drawing.Imaging.BitmapData bmpData=
bmp.LockBits(rect,System.Drawing.Imaging.ImageLockMode.ReadWrite,
像素格式);
//获取第一行的地址。
IntPtr ptr=bmpData.Scan0;
//声明一个数组以保存位图的字节。
int bytes=Math.Abs(bmpData.Stride)*bmp.Height;
字节[]rgbValues=新字节[字节];
//将RGB值复制到数组中。
System.Runtime.InteropServices.Marshal.Copy(ptr,rgbvalue,0,字节);
//将每三个值设置为255。24bpp位图将显示为红色。
对于(int counter=2;counter
您应该能够覆盖OnPaint方法

protected override void OnPaint(PaintEventArgs e)
{
    //base.OnPaint(e);
    LockUnlockBitsExample(e)
}

您需要输入已复制事件的代码!转到“属性”选项卡的“事件”窗格,双击“绘制”事件。@TaW,谢谢,这是一个我不知道的非常有用的功能!