C# code///
/// 剪裁 -- 用GDI+
///
/// 原始Bitmap
/// 开始坐标X
/// 开始坐标Y
/// 宽度
/// 高度
/// 剪裁后的Bitmap
public static Bitmap CutOut(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;
}
try
{
Bitmap bmpOut = new Bitmap(iWidth, iHeight, System.Drawing.Imaging.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;
}
catch
{
return null;
}
}
private void button1_Click(object sender, EventArgs e)
{
Image clean_bmp = this.pictureBox1.Image;
int r = 200;
int l = 100;
int b = 200;
int t = 100;
Bitmap bmp = CutOut(new Bitmap(clean_bmp), l, t, r, t);
bmp.Save(@"D:\a.tif");
}
------解决方案--------------------探讨
g.DrawImage(bmp, bmp_rect, l, t, r - l, b - t, GraphicsUnit.Pixel);
这个bmp不是你新建的空白画布吗?当然是空的默认一片黑了.实际上这个参数是源图,而不是目标图.
g.DrawImage(源图,目标图的哪个矩形,从源图中哪个矩形,模式)
此处源图为你要绘制的图,目标图为你新建的空白画布.