Winform實現圖片禎預覽
阿新 • • 發佈:2021-06-20
https://files.cnblogs.com/files/howld/FrameImageTool.zip
public partial class Form1 : Form { private MyImage myImage; private MyFrameImage myFrameImage; private Bitmap originImage; public Form1() { InitializeComponent(); //SetStyle(ControlStyles.UserPaint, true);//SetStyle(ControlStyles.AllPaintingInWmPaint, true); //SetStyle(ControlStyles.DoubleBuffer, true); originImage = new Bitmap(pictureBox1.Width, pictureBox1.Height); myImage = new MyImage(pictureBox1, originImage); myFrameImage = new MyFrameImage(pictureBox2, originImage); button1.Click+= button1_Click; textBox1.KeyPress += TextBox_KeyPress; textBox1.KeyDown += TextBox1_KeyDown; textBox2.KeyPress += TextBox_KeyPress; textBox2.KeyDown += TextBox2_KeyDown; textBox5.KeyPress += TextBox_KeyPress; textBox5.KeyDown+= TextBox5_KeyDown; checkBox1.CheckedChanged += CheckBox1_CheckedChanged; } private void CheckBox1_CheckedChanged(object sender, EventArgs e) { myFrameImage.timer.Enabled = checkBox1.Checked; } private void TextBox_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar != '\b') { if (e.KeyChar < '0' || e.KeyChar > '9') { e.Handled = true; return; } } else { TextBox textbox = sender as TextBox; if (textbox.Text.Length == 1) { textbox.Text = "0"; } } } private void TextBox1_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { myFrameImage.frameSize.Width = int.Parse(textBox1.Text); myFrameImage.DrawImage(); myImage.DrawImage(myFrameImage.frameSize); } } private void TextBox2_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { myFrameImage.frameSize.Height = int.Parse(textBox2.Text); myFrameImage.DrawImage(); myImage.DrawImage(myFrameImage.frameSize); } } private void TextBox5_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { myFrameImage.timer.Interval = int.Parse(textBox5.Text); } } // Load Image private void button1_Click(object sender, EventArgs e) { OpenFileDialog dialog = new OpenFileDialog(); if (dialog.ShowDialog() == DialogResult.OK) { if (originImage != null) { originImage.Dispose(); } Image image = Image.FromFile(dialog.FileName); originImage = new Bitmap(image.Width, image.Height); Graphics g = Graphics.FromImage(originImage); g.DrawImage(image, 0, 0); g.Dispose(); image.Dispose(); myImage.OriginImage = originImage; myImage.DrawImage(); myFrameImage.OriginImage = originImage; myFrameImage.DrawImage(); textBox1.Text = myFrameImage.frameSize.Width.ToString(); textBox2.Text = myFrameImage.frameSize.Height.ToString(); textBox5.Text = myFrameImage.timer.Interval.ToString(); Text = ("Image Size: " + originImage.Width.ToString() + "x" + originImage.Height.ToString()); } } }
public class MyBaseImage : IDisposable { protected PictureBox pictureBox; protected Bitmap originImage; protected Bitmap drawImage; protected Size imageSize; protected Point imagePosition; private bool moveImageFlag; private Point mouseDownPosition; public virtual Bitmap OriginImage { set { originImage = value; imageSize = value.Size; } } public MyBaseImage(PictureBox pictureBox, Bitmap originImage) { this.pictureBox = pictureBox; this.originImage = originImage; drawImage = new Bitmap(pictureBox.Width, pictureBox.Height); imageSize = originImage.Size; imagePosition.X = 0; imagePosition.Y = 0; moveImageFlag = false; pictureBox.MouseUp += PictureBox_MouseUp; pictureBox.MouseDown += PictureBox_MouseDown; pictureBox.MouseMove += PictureBox_MouseMove; pictureBox.MouseWheel += PictureBox_MouseWheel; } //~MyBaseImage() //{ // Dispose(false); //} public void Dispose() { Dispose(true); } public virtual void Dispose(bool disposing) { if (disposing) { drawImage.Dispose(); pictureBox.MouseUp -= PictureBox_MouseUp; pictureBox.MouseDown -= PictureBox_MouseDown; pictureBox.MouseMove -= PictureBox_MouseMove; pictureBox.MouseWheel -= PictureBox_MouseWheel; //GC.SuppressFinalize(this); } } public virtual void DrawImage() { } private void PictureBox_MouseUp(object sender, MouseEventArgs e) { moveImageFlag = false; } private void PictureBox_MouseDown(object sender, MouseEventArgs e) { moveImageFlag = true; mouseDownPosition = Control.MousePosition; } private void PictureBox_MouseMove(object sender, MouseEventArgs e) { if (moveImageFlag) { imagePosition.X += (Control.MousePosition.X - mouseDownPosition.X); imagePosition.Y += (Control.MousePosition.Y - mouseDownPosition.Y); mouseDownPosition = Control.MousePosition; DrawImage(); } } protected virtual void PictureBox_MouseWheel(object sender, MouseEventArgs e) { imageSize.Width += e.Delta; if (imageSize.Width <= 0) { imageSize.Width -= e.Delta; } imageSize.Height = originImage.Height * imageSize.Width / originImage.Width; DrawImage(); } } public class MyImage : MyBaseImage { private Bitmap copyImage; private Pen pen; public override Bitmap OriginImage { set { base.OriginImage = value; copyImage.Dispose(); copyImage = value.Clone() as Bitmap; } } public MyImage(PictureBox pictureBox, Bitmap originImage) : base(pictureBox, originImage) { copyImage = originImage.Clone() as Bitmap; pen = new Pen(Color.Blue, 1); } public override void Dispose(bool disposing) { copyImage.Dispose(); base.Dispose(disposing); } public override void DrawImage() { Graphics g = Graphics.FromImage(drawImage); g.Clear(pictureBox.BackColor); g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor; g.DrawImage(copyImage, imagePosition.X, imagePosition.Y, imageSize.Width, imageSize.Height); g.Dispose(); pictureBox.Image = drawImage; } public void DrawImage(Size frameSize) { DrawImage(frameSize.Width, frameSize.Height); } public void DrawImage(int frameWidth, int frameHeight) { //copyImage = originImage.Clone() as Bitmap; Graphics g = Graphics.FromImage(copyImage); g.Clear(pictureBox.BackColor); g.DrawImage(originImage, 0, 0); for (int x = 0; x <= originImage.Width; x += frameWidth) { g.DrawLine(pen, x, 0, x, originImage.Height); } for (int y = 0; y <= originImage.Height; y += frameHeight) { g.DrawLine(pen, 0, y, originImage.Width, y); } g.Dispose(); DrawImage(); } } public class MyFrameImage : MyBaseImage { public Size frameSize; private Point currentFrame; public Timer timer; //private int frameHCount; // private int frameVCount; public override Bitmap OriginImage { set { base.OriginImage = value; frameSize = value.Size; } } public MyFrameImage(PictureBox pictureBox, Bitmap originImage) : base(pictureBox, originImage) { frameSize = originImage.Size; currentFrame = new Point(0, 0); timer = new Timer(); timer.Interval = 200; timer.Tick += Timer_Tick; } protected override void PictureBox_MouseWheel(object sender, MouseEventArgs e) { imageSize.Width += e.Delta; if (imageSize.Width <= 0) { imageSize.Width -= e.Delta; } //imageSize.Height = frameSize.Height * imageSize.Width / frameSize.Width; DrawImage(); } public override void DrawImage() { imageSize.Height = frameSize.Height * imageSize.Width / frameSize.Width; Graphics g = Graphics.FromImage(drawImage); int x = currentFrame.X * frameSize.Width; int y = currentFrame.Y * frameSize.Height; g.Clear(pictureBox.BackColor); g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor; g.DrawImage(originImage, new Rectangle(imagePosition, imageSize), new Rectangle(x, y, frameSize.Width, frameSize.Height), GraphicsUnit.Pixel); g.Dispose(); pictureBox.Image = drawImage; } private void Timer_Tick(object sender, EventArgs e) { currentFrame.X += 1; if (currentFrame.X >= originImage.Width / frameSize.Width) { currentFrame.X = 0; currentFrame.Y += 1; if (currentFrame.Y >= originImage.Height / frameSize.Height) { currentFrame.X = 0; currentFrame.Y = 0; } } DrawImage(); } }