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 색을 그릴 사각형을 채 웁니다. 이 색상이 지워지지 않습니다. 직사각형 프레임 만. 나는 그것을 모두 지우고 원본 이미지 만 남겨두고 싶다.