查找要裁剪的位图的开始和结束位置 [英] Find Starting and Ending Position for an bitmap to Crop

查看:87
本文介绍了查找要裁剪的位图的开始和结束位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




在基于窗口的应用程序(C#.Net)中,我需要通过查找开始位置和结束位置来动态裁剪签名.

如果我有左上角点和右下角点,则可以通过形成一个矩形来裁剪图像.但是我的问题是如何在运行时使用这些数组字节查找那些点?

最终获得了裁剪图像的解决方案...

示例代码:

  public   byte  [] [] GetRGB(Bitmap bmp)
{
BitmapData bmp_data = bmp.LockBits(矩形( 0  0 ,bmp.Width,bmp.Height),ImageLockMode.ReadOnly,PixelFormat.Format24bppRgb);
 IntPtr  ptr = bmp_data.Scan0;
 int  num_pixels = bmp.Width * bmp.Height,num_bytes = bmp_data.Stride * bmp.Height,填充= bmp_data.Stride-bmp.Width *  0 ,ct = 字节 [] r =  字节 [ num_pixels],g =  字节 [num_pixels],b =  new  字节 [num_pixels],rgb =   0 ,num_bytes);

 for ( int  x =  0 ; x <  num_bytes- 3 ; x + =  3 )
{
 if (x ==(bmp_data.Stride * ct-padding)){x + = padding; ct ++; };
r [i] = rgb [x]; g [i] = rgb [x +  1 ]; b [i] = rgb [x +  2 ]; i ++;
}
bmp.UnlockBits(bmp_data);
返回  >字节 [ 3 ] [] {r,g,b};
}
公共图像自动裁剪(位图bmp)
{
// 获取包含每个像素的R,G,B分量的数组
 var 像素= GetRGB(bmp);

 int  h = bmp.Height- 1 ,w = bmp.Width,top =  0 ,白色=  0 ;
 int  tolerance =  95 ; //  95%

布尔 prev_color =  false ;
 for ( int  i =  0 ; i < 像素[ 0 ].Length; i ++)
{
 int  x =(i%(w)),y =( int )(Math.Floor(( 十进制)(i/w))),tol =  255  *公差/100;
如果(像素[ 0 ] [i] ">> /span> = tol&&像素[ 1 ] [i] >  = tol& & pixels [ 2 ] [i] >  = tol){white ++;右=(x > 右&&白色==  1 )? x:对; }
其他 {左=(x < 左&&白色 1 )吗? x:左;右=(x == w- 1 &&白色==  0 )? w- 1 :正确;白色=  0 ; }
如果(白色== w){top =(y-top <   1 &&y; > 顶部+  1 )? y:底部; }
左=(x ==  0 &&白色==  0 )?  0 :左侧; bottom =(y == h& x == w- 1 &&白色!= w& prev_color)? h +  1 :底部;
如果(x == w- 1 ){prev_color =(白色 true : false ;白色=  0 ; }
}
正确=(正确==  0 )? w:对;左=(左== w)  0 :左侧;

// 裁剪图像
位图bmpCrop = bmp.Clone(矩形(左,上,右-左+  1 ,底部-顶部),bmp.PixelFormat);

返回(位图)(bmpCrop);
} 




致电:

  var  new_image = AutoCrop( new 位图(  old.jpg"));
new_image.Save(" ,ImageFormat.Jpeg); 






参考:

Src:代码示例


Hi,


In my window based application(C#.Net) i need to crop a signature dynamically by finding the start and End Position.

If i have upper left corner point and lower right corner point then i can Clip the image by forming an rectangle.but my question is how to find those points in run time with those array bytes?

解决方案

Finally got Solution to Crop an image...

Sample Code :

public byte[][] GetRGB(Bitmap bmp)
{
	BitmapData bmp_data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
	IntPtr ptr = bmp_data.Scan0;
	int num_pixels = bmp.Width * bmp.Height, num_bytes = bmp_data.Stride * bmp.Height, padding = bmp_data.Stride - bmp.Width * 3, i = 0, ct = 1;
	byte[] r = new byte[num_pixels], g = new byte[num_pixels], b = new byte[num_pixels], rgb = new byte[num_bytes];
	System.Runtime.InteropServices.Marshal.Copy(ptr, rgb, 0, num_bytes);
	
	for (int x = 0; x < num_bytes - 3; x += 3)
	{
		if (x == (bmp_data.Stride * ct - padding)) { x += padding; ct++; };
		r[i] = rgb[x]; g[i] = rgb[x + 1]; b[i] = rgb[x + 2]; i++;
	}
	bmp.UnlockBits(bmp_data);
	return new byte[3][] { r, g, b };
}
public Image AutoCrop(Bitmap bmp)
{
	//Get an array containing the R,G,B components of each pixel
	var pixels = GetRGB(bmp);

	int h = bmp.Height - 1, w = bmp.Width, top = 0, bottom = h, left = bmp.Width, right = 0, white = 0;
	int tolerance = 95; // 95%

	bool prev_color = false;
	for (int i = 0; i < pixels[0].Length; i++)
	{
		int x = (i % (w)), y = (int)(Math.Floor((decimal)(i / w))), tol = 255 * tolerance/100; 
		if (pixels[0][i] >= tol && pixels[1][i] >= tol && pixels[2][i] >= tol) { white++; right = (x > right && white == 1) ? x : right; }
		else { left = (x < left && white >= 1) ? x : left; right = (x == w - 1 && white == 0) ? w - 1 : right; white = 0; }
		if (white == w) { top = (y - top < 3) ? y : top; bottom = (prev_color && x == w - 1 && y > top + 1) ? y : bottom; }
		left = (x == 0 && white == 0) ? 0 : left; bottom = (y == h && x == w - 1 && white != w && prev_color) ? h + 1 : bottom;
		if (x == w - 1) { prev_color = (white < w) ? true : false; white = 0; }
	}
	right = (right == 0) ? w : right; left = (left == w) ? 0 : left;

	//Crop the image
	Bitmap bmpCrop = bmp.Clone(new Rectangle(left, top, right - left + 1, bottom - top), bmp.PixelFormat);
	
	return (Bitmap)(bmpCrop);
}




To Call :

var new_image = AutoCrop(new Bitmap("old.jpg"));
new_image.Save("new.jpg",ImageFormat.Jpeg);






Reference :

Src : Code Sample


这篇关于查找要裁剪的位图的开始和结束位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆