目前,我正在使用面板来显示图像。我已经实现了通过鼠标缩放和移动图像的逻辑。我的问题是,当我缩放或移动图像时,我只想裁剪并保存面板上可见的图像部分。
这是我用来缩放和移动图像的代码,
namespace ImageZooming
{
public partial class Form2 : Form
{
public class overRidePanel : Panel
{
protected override void OnPaintBackground(PaintEventArgs pevent) { }
}
Bitmap bitmap;
BufferedGraphicsContext currentContext;
BufferedGraphics myBuffer;
PointF viewPortCenter;
float Zoom = 1.0f;
string sFilePath;
bool draging = false;
Point lastMouse;
int cropX;
int cropY;
byte[] MyData = new byte[0];
public Form2()
{
InitializeComponent();
this.panel1.MouseWheel += new MouseEventHandler(panel1_MouseWheel);
currentContext = BufferedGraphicsManager.Current;
setup(false);
}
private void setup(bool resetViewport)
{
if (myBuffer != null)
myBuffer.Dispose();
myBuffer = currentContext.Allocate(this.panel1.CreateGraphics(), this.panel1.DisplayRectangle);
if (bitmap != null)
{
if (resetViewport)
SetViewPort(new RectangleF(0, 0, bitmap.Width, bitmap.Height));
}
this.panel1.Focus();
this.panel1.Invalidate();
}
private void SetViewPort(RectangleF worldCords)
{
//Find smallest screen extent and zoom to that
if (worldCords.Height > worldCords.Width)
{
//Use With as limiting factor
this.Zoom = worldCords.Width / bitmap.Width;
}
else
this.Zoom = worldCords.Height / bitmap.Height;
viewPortCenter = new PointF(worldCords.X + (worldCords.Width / 2.0f), worldCords.Y + (worldCords.Height / 2.0f));
// this.toolStripStatusLabel1.Text = "Zoom: " + ((int)(this.Zoom * 100)).ToString() + "%";
}
private void SetViewPort(Rectangle screenCords)
{
}
private void PaintImage()
{
if (bitmap != null)
{
float widthZoomed = panel1.Width / Zoom;
float heigthZoomed = panel1.Height / Zoom;
//Do checks the reason 30,000 is used is because much over this will cause DrawImage to crash
if (widthZoomed > 30000.0f)
{
Zoom = panel1.Width / 30000.0f;
widthZoomed = 30000.0f;
}
if (heigthZoomed > 30000.0f)
{
Zoom = panel1.Height / 30000.0f;
heigthZoomed = 30000.0f;
}
//we stop at 2 because at this point you have almost zoomed into a single pixel
if (widthZoomed < 2.0f)
{
Zoom = panel1.Width / 2.0f;
widthZoomed = 2.0f;
}
if (heigthZoomed < 2.0f)
{
Zoom = panel1.Height / 2.0f;
heigthZoomed = 2.0f;
}
float wz2 = widthZoomed / 2.0f;
float hz2 = heigthZoomed / 2.0f;
Rectangle drawRect = new Rectangle(
(int)(viewPortCenter.X - wz2),
(int)(viewPortCenter.Y - hz2),
(int)(widthZoomed),
(int)(heigthZoomed));
//this.toolStripStatusLabel1.Text = "DrawRect = " + drawRect.ToString();
myBuffer.Graphics.Clear(Color.White); //Clear the Back buffer
//Draw the image, Write image to back buffer, and render back buffer
myBuffer.Graphics.DrawImage(bitmap, this.panel1.DisplayRectangle, drawRect, GraphicsUnit.Pixel);
myBuffer.Render(this.panel1.CreateGraphics());
// this.label1.Text = "Zoom: " + ((int)(this.Zoom * 100)).ToString() + "%"; //to show zooming %
}
}
private void Form1_Resize(object sender, EventArgs e)
{
setup(false);
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog fdg = new OpenFileDialog();
if (fdg.ShowDialog() == DialogResult.OK)
{
bitmap = (Bitmap)Bitmap.FromFile(fdg.FileName);
setup(true);
}
sFilePath = fdg.FileName;
}
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
draging = true;
}
// public Pen cropPen;
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
//cropPen=new Pen(Color.Black,1);
//cropX = e.X;
//cropY = e.Y;
if (draging)
{
viewPortCenter = new PointF(viewPortCenter.X + ((lastMouse.X - e.X) / Zoom), viewPortCenter.Y + ((lastMouse.Y - e.Y) / Zoom));
panel1.Invalidate();
}
//panel1.CreateGraphics().DrawRectangle(cropPen, 0, 0, panel1.Width, panel1.Height);
lastMouse = e.Location;
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
draging = false;
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
PaintImage();
}
private void panel1_MouseWheel(object sender, System.Windows.Forms.MouseEventArgs e)
{
Zoom += Zoom * (e.Delta / 1200.0f);
if (e.Delta > 0 && e.Delta < 0)
viewPortCenter = new PointF(viewPortCenter.X + ((e.X - (panel1.Width / 2)) / (2 * Zoom)), viewPortCenter.Y + ((e.Y - (panel1.Height / 2)) / (2 * Zoom)));
this.panel1.Invalidate();
}