2014-09-23 6 views
-1
using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.Drawing.Imaging; 
using System.Runtime.InteropServices; 

namespace mws 
{ 
    public partial class ColorClouds : Form 
    { 
     private Bitmap _bmpBU = null; 
     private Rectangle mRect; 

     public ColorClouds() 
     { 
      InitializeComponent(); 

      this.DoubleBuffered = true; 
      _bmpBU = new Bitmap(@"D:\MyWeatherStation-Images-And-Icons\radar090.PNG"); 
     } 

     private void ColorClouds_Load(object sender, EventArgs e) 
     { 
      this.pictureBox1.Image = (Bitmap)_bmpBU.Clone(); 
      this.pictureBox2.Image = (Bitmap)_bmpBU.Clone(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      if (pictureBox1.Image != null) 
      { 
       ScanBmpFast((Bitmap)this.pictureBox1.Image, true, new Rectangle(40, 10, 20, 50)); 
       this.pictureBox1.Refresh(); 
      } 
     } 

     private unsafe void ScanBmpFast(Bitmap bmp, bool setToOpaqueYellow, Rectangle rect) 
     { 
      BitmapData b1 = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppArgb); 

      int stride = b1.Stride; 
      int nWidth = bmp.Width; 
      int nHeight = bmp.Height; 

      System.IntPtr Scan0 = b1.Scan0; 

      byte* p = (byte*)(void*)Scan0; 

      int sX = Math.Min(Math.Max(rect.X, 0), bmp.Width); 
      int sY = Math.Min(Math.Max(rect.Y, 0), bmp.Height); 
      int w = Math.Min(rect.Width, bmp.Width - sX); 
      int h = Math.Min(rect.Height, bmp.Height - sY); 

      for (int y = sY; y < sY + h; y++) 
      { 
       p = (byte*)(void*)Scan0; 
       p += (y * stride + sX * 4); 

       for (int x = sX; x < sX + w; x++) 
       { 
        if (p[0] != 0 || p[1] != 0 || p[2] != 0) 
        { 
         p[1] = p[2] = (byte)255; 

         if (setToOpaqueYellow) 
         { 
          p[0] = (byte)0; 
          p[3] = (byte)255; 
         } 
        } 

        p += 4; 
       } 
      } 

      bmp.UnlockBits(b1); 
     } 

     private void button2_Click(object sender, EventArgs e) 
     { 
      if (pictureBox2.Image != null) 
      { 
       ScanBmp((Bitmap)this.pictureBox2.Image, true, new Rectangle(400, 100, 200, 500)); 
       this.pictureBox2.Refresh(); 
      } 
     } 

     private void ScanBmp(Bitmap bmp, bool setToOpaqueYellow, Rectangle rect) 
     { 
      BitmapData b1 = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppArgb); 

      int stride = b1.Stride; 
      int nWidth = bmp.Width; 
      int nHeight = bmp.Height; 

      System.IntPtr Scan0 = b1.Scan0; 
      IntPtr p = b1.Scan0; 

      int sX = Math.Min(Math.Max(rect.X, 0), bmp.Width); 
      int sY = Math.Min(Math.Max(rect.Y, 0), bmp.Height); 
      int w = Math.Min(rect.Width, bmp.Width - sX); 
      int h = Math.Min(rect.Height, bmp.Height - sY); 

      for (int y = sY; y < sY + h; y++) 
      { 
       p = Scan0; 
       p += y * stride + sX * 4; 

       for (int x = sX; x < sX + w; x++) 
       { 

        if (Marshal.ReadByte(p) != 0 || Marshal.ReadByte(p + 1) != 0 || Marshal.ReadByte(p + 2) != 0) 
        { 
         byte res = (byte)255; 
         Marshal.WriteByte(p + 1, res); 
         Marshal.WriteByte(p + 2, res); 

         if (setToOpaqueYellow) 
         { 
          Marshal.WriteByte(p, (byte)0); 
          Marshal.WriteByte(p + 2, (byte)255); 
         } 
        } 

        p += 4; 
       } 
      } 

      bmp.UnlockBits(b1); 
     } 

     private void button3_Click(object sender, EventArgs e) 
     { 
      Image iOLd = this.pictureBox1.Image; 
      this.pictureBox1.Image = (Bitmap)_bmpBU.Clone(); 
      if (iOLd != null) 
       iOLd.Dispose(); 
     } 

     private void button4_Click(object sender, EventArgs e) 
     { 
      Image iOLd = this.pictureBox2.Image; 
      this.pictureBox2.Image = _bmpBU; 
      if (iOLd != null) 
       iOLd.Dispose(); 
     } 

     private void ColorClouds_FormClosing(object sender, FormClosingEventArgs e) 
     { 
      Image iOLd = this.pictureBox1.Image; 
      this.pictureBox1.Image = (Bitmap)_bmpBU.Clone(); 
      if (iOLd != null) 
       iOLd.Dispose(); 
      iOLd = this.pictureBox2.Image; 
      this.pictureBox2.Image = _bmpBU; 
      if (iOLd != null) 
       iOLd.Dispose(); 

      if (_bmpBU != null) 
       _bmpBU.Dispose(); 
     } 

     private void pictureBox1_Paint(object sender, PaintEventArgs e) 
     { 
      using (Pen pen = new Pen(Color.Red, 2)) 
      { 
       e.Graphics.DrawRectangle(pen, mRect); 
      } 
     } 

     private void pictureBox1_MouseMove(object sender, MouseEventArgs e) 
     { 
      if (e.Button == MouseButtons.Left) 
      { 
       mRect = new Rectangle(mRect.Left, mRect.Top, e.X - mRect.Left, e.Y - mRect.Top); 
       pictureBox1.Invalidate(); 
      } 
     } 

     private void pictureBox1_MouseDown(object sender, MouseEventArgs e) 
     { 
      mRect = new Rectangle(e.X, e.Y, 0, 0); 
     } 

     private void pictureBox1_MouseUp(object sender, MouseEventArgs e) 
     { 
      if (pictureBox1.Image != null) 
      { 
       ScanBmpFast((Bitmap)this.pictureBox1.Image, true, mRect); 
       this.pictureBox1.Refresh(); 
      } 
     } 
    } 
} 

나는 picturebox1 위에 사각형을 그리고 직사각형으로 채울 수 있습니다. 다시 그리기 위해 pictureBox1을 클릭하면 직사각형 프레임이 선명하지만 직사각형 내부의 자체 드로잉은 아닙니다.원본 이미지를 지우지 않고 pictureBox1에서 그려진 그래픽을 모두 지우려면 어떻게합니까?

ScanBmpFast 그냥 i 색을 그릴 사각형을 채 웁니다. 이 색상이 지워지지 않습니다. 직사각형 프레임 만. 나는 그것을 모두 지우고 원본 이미지 만 남겨두고 싶다.

답변

0

한숨. Paint (직접 또는 그 주위를 지나칠 수있는 e.Graphics)에 그림을 그려 넣지 않으면 아무 것도 지속되지 않습니다.

그래서 당신이 그 비 영구적 인 픽셀 없애 할 필요는 물론 문제로 연결

pictureBox1.Invalidate(); 

를 호출하는 것입니다 당신이 그리는 당신은 바로 그것을 왜하지 않는 것 ? Invalidate()Form 등을 최소화처럼, 코드뿐만 아니라 많은 외부 이벤트에 의해 당신에게 전화하여 Paint 이벤트를 벗어난 건은 .. 삭제되지 않습니다

(당신이 Image로 그려하지 않는 한) 데이터를 생성해야합니다 그리기 영구 당신이 Rectangle을하지 않으려면 그려 질 그래서 ..

무엇의 세부 사항을 보유 구조는 어디에 그리는 방법, 즉 Points, Rectangles, Brushes, Pens

그리고 그 데이터 분명히 당신을 그리기 지속적 등을 삭제 Paint 이벤트에서하지 마십시오! 코드가 있으면 그 코드가 항상 다시 나타납니다.

관련 문제