解决方案 »
- 请教 菜单项目 的命名规范,即菜单项应该如何起名才规范呀?
- 请教关于Winform 的Webbrowser 编辑器问题?
- 关于c# treeview控件 救救命吧!!万分感谢!!!
- 关于 SpeechLib.SpVoice的EndStream事件
- 请问如何取得TabControl中TabPage 里的一个 Label控件名称
- C#编写磁盘扫描程序
- 字符串问题
- Clipboard.SetDataObject为什么显示错误啊?
- 高手指点下这个EXCle怎样用datable填写
- GDI+中如何实现GDI中setROP2的功能(C#)?
- C#.NET如何改变已显示出来的TreeNode的标签文字(Text)
- 收藏 不显示删除回复显示所有回复显示星级回复显示得分回复 如何在 usercontrol中把一个按钮设置成默认的,就是一回车就执行这个按钮
#region 剪裁掉边框
/// <summary>
/// 判断指定行内像素是否与指定颜色都相同
/// </summary>
/// <param name="setColor">指定色</param>
/// <param name="bmp">原始图</param>
/// <param name="lineIndex">行号</param>
/// <param name="tolerance">容差</param>
/// <returns>是/否</returns>
private bool AllPixelsAreSameColor(Color setColor, Bitmap bmp, int lineIndex, int tolerance)
{
bool rtn = false; int w = bmp.Width;
int h = bmp.Height; //取一行
BitmapData data = bmp.LockBits(new Rectangle(0, lineIndex, w, 1), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); unsafe
{
byte* p = (byte*)data.Scan0; for (int x = 0; x < w; x++)
{
// 检验指定位置的像素值
Color curColor = Color.FromArgb(p[2],p[1],p[0]); if (ColorIsSimilar(setColor, curColor, tolerance))
{
rtn = true;
}
else
{
rtn = false;
break;
} p += 3;
} // end of x } bmp.UnlockBits(data); return rtn; } /// <summary>
/// 颜色是否近似
/// </summary>
/// <param name="c0">颜色0</param>
/// <param name="c1">颜色1</param>
/// <param name="delta">容差</param>
/// <returns>是/否</returns>
private bool ColorIsSimilar(Color c0, Color c1, int delta)
{
int r0, r1, g0, g1, b0, b1; r0 = c0.R;
r1 = c1.R; g0 = c0.G;
g1 = c1.G; b0 = c0.B;
b1 = c1.B; if ((r0 - r1) * (r0 - r1) + (g0 - g1) * (g0 - g1) + (b0 - b1) * (b0 - b1) <= delta * delta)
{
return true;
}
else
{
return false;
} } /// <summary>
/// 剪裁
/// </summary>
/// <param name="b">原始Bitmap</param>
/// <param name="StartX">开始坐标X</param>
/// <param name="StartY">开始坐标Y</param>
/// <param name="iWidth">宽度</param>
/// <param name="iHeight">高度</param>
/// <returns>剪裁后的Bitmap</returns>
private 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;
} Graphics g = null; try
{
Bitmap bmpOut = new Bitmap(iWidth, iHeight, PixelFormat.Format24bppRgb); g = Graphics.FromImage(bmpOut);
g.DrawImage(b, new Rectangle(0, 0, iWidth, iHeight), new Rectangle(startX, startY, iWidth, iHeight), GraphicsUnit.Pixel); return bmpOut;
}
catch
{
return null;
}
finally
{
if (g != null) g.Dispose();
}
}
/// <summary>
/// 切除周围的指定颜色
/// </summary>
/// <param name="bmp">原始图</param>
/// <param name="c">指定色</param>
/// <param name="tolerance">颜色容差</param>
/// <returns>切除后的图片</returns>
public Bitmap CutAround(Bitmap bmp, Color c, int tolerance)
{
if (bmp == null) return null; bool CutFlag = false;
int i = 0; int w = bmp.Width;
int h = bmp.Height; // 扫描每一行
for (i = 0; i < h; i++)
{
// 如果发现此行并非同一个颜色则退出
if (!AllPixelsAreSameColor(c, bmp, i, tolerance))
{
CutFlag = true;
break;
}
} // 剪裁
if (CutFlag) bmp = KiCut(bmp, 0, i, w, h - i); return bmp;
} #endregion
{
Bitmap bimage = new Bitmap(image.Width, image.Height);
bool bwhy;
int tmph;
for (int i = 0; i < image.Width; i++)
{
for (int b = 0; b < image.Height; b++)
{
bimage.SetPixel( i, b,Color.FromArgb(255, 255, 255));
}
}
for (int i = 0; i < image.Width; i++)
{
tmph = 0;
bwhy = false; for (int b = 0; b < image.Height; b++)
{
if (image.GetPixel(i, b).R < 220&&bwhy==false)
{
bwhy = true;
tmph = b;
} if (bwhy)
{
bimage.SetPixel(i, b - tmph, image.GetPixel(i, b));
}
}
} this.pictureBox1.Image = bimage;
}
哈哈这是我根据楼上代码昨晚通宵研究出来的.经测试可行.哪位网友可以帮我再修改优化下代码以提升性能吗?