内存流字节数组与ImageConverter字节数组

时间:2015-03-09 14:28:03

标签: c# bytearray memorystream

我正在将图像转换为字节数组,然后将字节转换为我的epson打印机可以打印的内容

这段代码将图片快速转换为字节

ImageConverter converter = new ImageConverter();
        buff = (byte[])converter.ConvertTo(signature, typeof(byte[]));

这个很慢但有效

public static byte[] ImageToByte2(Image img)
    {
        byte[] byteArray = new byte[0];
        using (MemoryStream stream = new MemoryStream())
        {
            img.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
            stream.Close();

            byteArray = stream.ToArray();
        }
        return byteArray;
    }

这两个都导致了一个不同的字节数组,我无法弄清楚为什么,内存流一个是适合我但转换速度慢的一个

任何人都可以暗示为什么这些字节数组不同,转换相同的图片

ex图像转换器字节数组 Image Converter

ex memorystream conv

Memorystream Conversion

 string display = BitConverter.ToString(buff);

        MessageBox.Show(display);


Bitmap signature;
pictureBox1.Image=signature;
string Base64String = "79,27:79,27:79,26:79,26:79,26:79,27:79,28:77,30:73,33:69,38:66,44:62,52:56,62:48,72:42,79:39,82:39,81:#,#:79,29:79,29:78,28:80,26:82,26:81,30:77,36:75,40:74,44:75,47:76,50:78,51:84,48:92,41:101,33:108,27:110,26:109,26:109,28:109,30:109,31:109,34:107,40:103,49:97,58:93,65:88,71:87,74:#,#:133,38:133,38:133,38:132,40:129,47:124,54:121,59:117,65:114,70:#,#:130,39:130,39:130,39:135,35:145,29:157,24:166,23:#,#:125,52:125,52:122,53:125,52:136,48:147,47:152,48:#,#:114,69:114,69:112,69:117,68:129,65:139,63:147,62:#,#:199,30:199,30:197,31:196,35:195,40:195,45:191,54:182,63:174,72:171,76:173,76:182,72:198,69:216,67:231,66:239,66:240,67:238,68:#,#:";



        string[] cord = Base64String.Split(new Char[]{',',':'},StringSplitOptions.RemoveEmptyEntries);
        Graphics g;

        g = Graphics.FromImage(signature);
        g.Clear(Color.White);

        Pen mypen = new Pen(new SolidBrush(Color.Black));
        mypen.Width = 2;
        mypen.EndCap = System.Drawing.Drawing2D.LineCap.Square;
        mypen.StartCap = System.Drawing.Drawing2D.LineCap.Square;
        mypen.MiterLimit = mypen.Width * 1.25f;
        mypen.LineJoin = System.Drawing.Drawing2D.LineJoin.Round;
        for (int i = 0; i < cord.Length; i=i+4)
        {
            //MessageBox.Show(cord[i]);
            Point p;
            Point p2;
            if (cord[i] != "#" && cord[i + 1] != "#")
            {
                p = new Point(int.Parse(cord[i]), int.Parse(cord[i + 1]));


                if (cord[i + 2] != "#" && cord[i + 3] != "#")
                {
                    p2 = new Point(int.Parse(cord[i + 2]), int.Parse(cord[i + 3]));
                }
                else
                {
                    p2 = new Point(int.Parse(cord[i]), int.Parse(cord[i + 1]));

                }

            }
            else
            {

            }




        }

        g.Dispose();
        signature = BitmapTo1Bpp(signature);

public static Bitmap BitmapTo1Bpp(Bitmap img)
    {
        int w = img.Width;
        int h = img.Height;
        Bitmap bmp = new Bitmap(w, h, PixelFormat.Format1bppIndexed);
        BitmapData data = bmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadWrite, PixelFormat.Format1bppIndexed);
        byte[] scan = new byte[(w + 7) / 8];
        for (int y = 0; y < h; y++)
        {
            for (int x = 0; x < w; x++)
            {
                if (x % 8 == 0) scan[x / 8] = 0;
                Color c = img.GetPixel(x, y);
                if (c.GetBrightness() >= 0.5) scan[x / 8] |= (byte)(0x80 >> (x % 8));
            }
            Marshal.Copy(scan, 0, (IntPtr)((long)data.Scan0 + data.Stride * y), scan.Length);
        }
        bmp.UnlockBits(data);
        return bmp;
    }

1 个答案:

答案 0 :(得分:2)

根据找到的here的MSDN文档,ImageConverter函数将图像对象引用的内部 .NET表示转换为字节数组,而不是它包含的图像数据。 / p>