g1 当作源,designer1 是什么东西啊?
所谓的失真是颜色不对图像对,还是坐标不对?还是全黑的?
Bitmap.Clone 可以复制一副图像的一部分,不需要再去用 BitBlt

解决方案 »

  1.   

    经过测试截图本身不失真,是在转换为单色图时失真了,这个问题各位大神遇到过没?

    你这个叫二值化,不叫做单色图。
    找本基础的书看看什么叫单色图吧。  /// <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);        }
    大神,这个是我的转换方法, 出来后图片严重失真, 请问有什么办法解决吗?
      

  2.   

    我现在加了调整灰度的算法了,怎么还是不行呢? 大神???
     /// <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);        }
      

  3.   

    原图是什么样的,发出来看看
    如果字中间的条跟背景色本来就是完全一样的,你做什么处理都没用
    如果是有区别,但是区别不明显,你需要计算字主体的平均值,然后用这个值去调整
    我看你直接传了个int v进去用来调整,但是对于不同的图像,这个平均值其实是不同的