2016-02-12 101 views
-1

我創建了一個表格形式的油漆事件,並想利用這個代碼示例:如何處理

https://msdn.microsoft.com/en-us/library/5ey6h79d%28v=vs.110%29.aspx

與代碼的指令是:

要運行這個例如,將其粘貼到表單中,並通過調用LockUnlockBitsExample方法處理表單的Paint事件,並將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(); 
     } 

    } 
} 
+0

您需要[掛鉤(http://stackoverflow.com/questions/33275763/copy-datagridview-values-to- textbox/33276161?s = 2 | 0.5622#33276161)您複製的事件的代碼!轉到屬性選項卡的事件窗格,然後雙擊Paint事件。 – TaW

+0

@TaW,謝謝,這是一個非常有用的功能,我不知道! – rwvb

回答

1

你應該能夠覆蓋OnPaint方法

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