我想截取一个图片的以正中心为中心,截取一个1000个像素的正方形区域的图片,显示在picture2控件上,结果全是黑的
public static Bitmap Cut(Bitmap b, int StartX, int StartY,int endX,int endY, int iWidth, int iHeight)
{
if (b ==null)
{
return null;
}
int w = b.Width;
int h = b.Height;
StartX = (w / 2)-500;
StartY = (h / 2)-500;
endX = (w / 2) + 500;
endY = (w / 2) + 500;
iWidth = 1000;
iHeight = 1000;
Bitmap bitOut = new Bitmap(iWidth, iHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
Graphics g = Graphics.FromImage(bitOut);
g.DrawImage(b, new Rectangle(StartX, StartY, endX,endY), new Rectangle(0, 0, iWidth, iHeight),GraphicsUnit.Pixel);
g.Dispose();
return bitOut;
}
private void button1_Click(object sender, EventArgs e)
{
pictureBox1.Image = Image.FromFile(@"C:\r.jpg");
Bitmap clean_bmp = new Bitmap(@"C:\r.jpg");
textBox1.Text = clean_bmp.Width.ToString();
textBox2.Text = clean_bmp.Height.ToString();
int r = 200;
int l = 100;
int b = 200;
int t = 100;
Bitmap bmp = Cut(clean_bmp, r, l, b, t,1000,1000);
pictureBox2.Image = bmp;
}
public static Bitmap Cut(Bitmap b, int StartX, int StartY,int endX,int endY, int iWidth, int iHeight)
{
if (b ==null)
{
return null;
}
int w = b.Width;
int h = b.Height;
StartX = (w / 2)-500;
StartY = (h / 2)-500;
endX = (w / 2) + 500;
endY = (w / 2) + 500;
iWidth = 1000;
iHeight = 1000;
Bitmap bitOut = new Bitmap(iWidth, iHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
Graphics g = Graphics.FromImage(bitOut);
g.DrawImage(b, new Rectangle(StartX, StartY, endX,endY), new Rectangle(0, 0, iWidth, iHeight),GraphicsUnit.Pixel);
g.Dispose();
return bitOut;
}
private void button1_Click(object sender, EventArgs e)
{
pictureBox1.Image = Image.FromFile(@"C:\r.jpg");
Bitmap clean_bmp = new Bitmap(@"C:\r.jpg");
textBox1.Text = clean_bmp.Width.ToString();
textBox2.Text = clean_bmp.Height.ToString();
int r = 200;
int l = 100;
int b = 200;
int t = 100;
Bitmap bmp = Cut(clean_bmp, r, l, b, t,1000,1000);
pictureBox2.Image = bmp;
}
public static Bitmap Cut(Image bitmap, int width, int height)
{
if (bitmap == null)
{
return null; } width = Math.Min(bitmap.Width, width);//确保图片够大
height = Math.Min(bitmap.Height, height);
width = height = Math.Min(width, height);//正方形
Bitmap retBitmap = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
Graphics g = Graphics.FromImage(retBitmap);
g.DrawImage(bitmap, 0,0, new Rectangle((bitmap.Width - width)/2,(bitmap.Height - height)/2, width, height) , GraphicsUnit.Pixel);
g.Dispose();
return retBitmap;
}
private void button1_Click(object sender, EventArgs e)
{
pictureBox1.Image = Image.FromFile(@"C:/Documents and Settings/Administrator/My Documents/My Pictures/ReneLiu.jpg");
Bitmap bmp = Cut(pictureBox1.Image, 1000, 1000);
pictureBox2.Image = bmp; }