图像框选放大和缩小(在线等)
如何实现框选图片的某个部位
然后放大或者缩小?高手指点,急用
------解决方案--------------------框选
private GraphicsPath gp = new GraphicsPath();
MouseMove:
preview.Refresh();
gp.Reset();
gp.AddRectangle(new Rectangle(p0.X, p0.Y, Math.Abs(p0.X - p1.X), Math.Abs(p0.Y - p1.Y)));
Graphics g = preview.CreateGraphics();
Pen pen = new Pen(Color.Red, 1.0f);
g.DrawPath(pen, gp);
g.Dispose();
MouseDown:
preview.Refresh();
gp.Reset();
需要注意处理起始点和终止点的位置问题(比如,用户从右下拖到左上)
框选好以后你可以用Graphics类的DrawImage得到框选好的图像。同样还是DrawImage实现放大缩小。
//剪裁
public static Bitmap KiCut(Bitmap b, int StartX, int StartY, int iWidth, int iHeight)
{
if (b == null)
{
return null;
}
int w = b.Width;
int h = b.Height;
if (StartX > = w || StartY > = h)
{
return null;
}
if (StartX + iWidth > w)
{
iWidth = w - StartX;
}
if (StartY + iHeight > h)
{
iHeight = h - StartY;
}
Bitmap bmpOut = new Bitmap(iWidth, iHeight, PixelFormat.Format24bppRgb);
Graphics g = Graphics.FromImage(bmpOut);
g.DrawImage(b, new Rectangle(0, 0, iWidth, iHeight), new Rectangle(StartX, StartY, iWidth, iHeight), GraphicsUnit.Pixel);
g.Dispose();
return bmpOut;
}
//重设大小
public static Bitmap KiResizeImage(Bitmap bmp, int newW, int newH)
{
try
{
Bitmap b = new Bitmap(newW, newH);
Graphics g = Graphics.FromImage(b);
//强制用效果最好的插值算法
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(bmp, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);
g.Dispose();
return b;
}
catch
{
return null;
}
}
需要的话,最后再把得到的Bitmap重新画到PictureBox里。