g1 当作源,designer1 是什么东西啊?
所谓的失真是颜色不对图像对,还是坐标不对?还是全黑的?
Bitmap.Clone 可以复制一副图像的一部分,不需要再去用 BitBlt
所谓的失真是颜色不对图像对,还是坐标不对?还是全黑的?
Bitmap.Clone 可以复制一副图像的一部分,不需要再去用 BitBlt
解决方案 »
- 【WPF】 textbox在输入文本时弹出一个靠右的popup,textbox和popup都有输入焦点,怎么做?
- 求助热心的CSDN坛友,关于WebApplication的求助,初学者问题多多啊~~
- 紧急求助, scrollbar 不受控制了
- 未将对象的引用设置到对象的实例?在线等!
- DataGridView 取第一行的时候提示索引超出范围,其他行并无错误
- 初级数据库建立及存储及使用及学习及问题
- 窗体接收键盘事件
- C#的线程问题
- 请问在知道某个汉字或非常规ASC字符的Asc值后,如何根据这个ASC值重新得到这个字符?!
- 一个小问题,希望各位大虾帮我一下,在线等待!!!
- 椭圆文字环排算法求完善
- c#做数据对调,餐饮系统换桌怎么实现数据对调求指点
你这个叫二值化,不叫做单色图。
找本基础的书看看什么叫单色图吧。 /// <summary>
/// 将图片转单色位图
/// </summary>
/// <param name="matrix">预处理的文件</param>
/// <param name="v">预转换的色位(8,128,256)</param>
/// <param name="path">保存图片路径</param>
/// <returns></returns> public void toBitmap(Bitmap matrix, int v, string path)
{
//Bitmap matrix = new Bitmap(imagePath);
int width = matrix.Width;
int height = matrix.Height;
Bitmap bmp = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format1bppIndexed);
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, bmp.PixelFormat); IntPtr ptr = bmpData.Scan0; int sWidth = Math.Abs(bmpData.Stride);
int sDepth = 8;
int bytes = sWidth * bmp.Height;
// System.Diagnostics.Debug.WriteLine(string.Format("宽度:{0},1个字符等于{1}像素", sWidth, sDepth));
// System.Diagnostics.Debug.WriteLine(string.Format("文件大小:{0}", bytes));
byte[] rgbValues = new byte[bytes];
int r, g, b;
int sColor = 0;
System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);
for (int h = 0; h < bmp.Height; h++)
{
for (int w = 0; w < sWidth; w++)
{
sColor = 0;
for (int i = 0; i < sDepth; i++)
{
if ((i + w * sDepth) >= bmp.Width)
{
break;
}
r = matrix.GetPixel(i + w * sDepth, h).R;
g = matrix.GetPixel(i + w * sDepth, h).G;
b = matrix.GetPixel(i + w * sDepth, h).B;
sColor += (byte)(((byte)(0.2125 * r + 0.7154 * g + 0.0721 * b) >= v) ? (128 >> (i)) : 0);
}
rgbValues[sWidth * h + w] = (byte)sColor;
}
}
System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);
bmp.UnlockBits(bmpData);
bmp.Save(path, System.Drawing.Imaging.ImageFormat.Bmp); }
大神,这个是我的转换方法, 出来后图片严重失真, 请问有什么办法解决吗?
/// <summary>
/// 图像灰度化
/// </summary>
/// <param name="bmp"></param>
/// <returns></returns>
public void ToGray(Bitmap bmp, string path)
{
for (int i = 0; i < bmp.Width; i++)
{
for (int j = 0; j < bmp.Height; j++)
{
//获取该点的像素的RGB的颜色
Color color = bmp.GetPixel(i, j);
//利用公式计算灰度值
int gray = (int)(color.R * 0.3 + color.G * 0.59 + color.B * 0.11);
Color newColor = Color.FromArgb(gray, gray, gray);
bmp.SetPixel(i, j, newColor);
}
}
toBitmap(bmp,8,path);
} /// <summary>
/// 将图片转单色位图
/// </summary>
/// <param name="matrix">预处理的文件</param>
/// <param name="v">预转换的色位(8,128,256)</param>
/// <param name="path">保存图片路径</param>
/// <returns></returns> public void toBitmap(Bitmap matrix, int v, string path)
{
//Bitmap matrix = new Bitmap(imagePath);
int width = matrix.Width;
int height = matrix.Height;
Bitmap bmp = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format1bppIndexed);
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, bmp.PixelFormat); IntPtr ptr = bmpData.Scan0; int sWidth = Math.Abs(bmpData.Stride);
int sDepth = 8;
int bytes = sWidth * bmp.Height;
// System.Diagnostics.Debug.WriteLine(string.Format("宽度:{0},1个字符等于{1}像素", sWidth, sDepth));
// System.Diagnostics.Debug.WriteLine(string.Format("文件大小:{0}", bytes));
byte[] rgbValues = new byte[bytes];
int r, g, b;
int sColor = 0;
System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);
for (int h = 0; h < bmp.Height; h++)
{
for (int w = 0; w < sWidth; w++)
{
sColor = 0;
for (int i = 0; i < sDepth; i++)
{
if ((i + w * sDepth) >= bmp.Width)
{
break;
}
r = matrix.GetPixel(i + w * sDepth, h).R;
g = matrix.GetPixel(i + w * sDepth, h).G;
b = matrix.GetPixel(i + w * sDepth, h).B;
sColor += (byte)(((byte)(0.2125 * r + 0.7154 * g + 0.0721 * b) >= v) ? (128 >> (i)) : 0);
}
rgbValues[sWidth * h + w] = (byte)sColor;
}
}
System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);
bmp.UnlockBits(bmpData);
bmp.Save(path, System.Drawing.Imaging.ImageFormat.Bmp); }
如果字中间的条跟背景色本来就是完全一样的,你做什么处理都没用
如果是有区别,但是区别不明显,你需要计算字主体的平均值,然后用这个值去调整
我看你直接传了个int v进去用来调整,但是对于不同的图像,这个平均值其实是不同的