Xamarin表单的System.Drawing.Bitmap的替代方案

时间:2019-07-04 19:47:35

标签: forms class xamarin bitmap

我需要使用System.Drawing.Bitmap中的Bitmap类,这是一个在Windows Platform上正常运行的函数。但是,当我尝试在Xamarin Forms上运行并安装了nuget软件包System.Drawing之后,该程序可以正确编译而不会出现错误。

但是在运行程序时收到错误消息。似乎以某种方式指向Windows中的System.Drawing,而不是nuget包中的System.Drawing

我需要做的是,从相机获取照片并打印。 下面是要打印的代码。问题出在“位图”转换器上。

尝试了几个nuget软件包,但没有一个起作用: System.Drawing.Common 快速位图 位图网

   public byte[] PrintImage(byte[] PHOTO)
    {
        Bitmap bmp;
        using (var ms = new MemoryStream(PHOTO))
        {
            bmp = new Bitmap(ms);
        }

        BitmapData data = GetBitmapData(bmp); 
        BitArray dots = data.Dots;
        byte[] width = BitConverter.GetBytes(data.Width);

        int offset = 0;
        MemoryStream stream = new MemoryStream();
        BinaryWriter bw = new BinaryWriter(stream);

        // center command
        bw.Write(27);
        bw.Write('a');
        bw.Write(1);

        // print image
        bw.Write((char)0x1B);
        bw.Write('@');

        bw.Write((char)0x1B);
        bw.Write('3');
        bw.Write((byte)24);

        while (offset < data.Height)
        {
            bw.Write((char)0x1B);
            bw.Write('*');         // bit-image mode
            bw.Write((byte)33);    // 24-dot double-density
            bw.Write(width[0]);  // width low byte
            bw.Write(width[1]);  // width high byte

            for (int x = 0; x < data.Width; ++x)
            {
                for (int k = 0; k < 3; ++k)
                {
                    byte slice = 0;
                    for (int b = 0; b < 8; ++b)
                    {
                        int y = (((offset / 8) + k) * 8) + b;
                        // Calculate the location of the pixel.
                        // It'll be at (y * width) + x.
                        int i = (y * data.Width) + x;

                        // If the image is shorter than 24 dots.
                        bool v = false;
                        if (i < dots.Length)
                        {
                            v = dots[i];
                        }
                        slice |= (byte)((v ? 1 : 0) << (7 - b));
                    }

                    bw.Write(slice);
                }
            }
            offset += 24;
            bw.Write((char)0x0A);
        }
        // Restore the line spacing to the default of 30 dots.
        bw.Write((char)0x1B);
        bw.Write('3');
        bw.Write((byte)30);

        bw.Flush();
        byte[] bytes = stream.ToArray();
        return bytes;       // logo + Encoding.Default.GetString(bytes);
    }

    public BitmapData GetBitmapData(Bitmap bmp) // (string bmpFileName)
    {
        //using (var bitmap = (Bitmap)Bitmap.FromFile(bmpFileName))
        using (var bitmap = bmp)
        {
            var threshold = 127;
            var index = 0;
            double multiplier = 570; // this depends on your printer
            double scale = (double)(multiplier / (double)bitmap.Width);
            int xheight = (int)(bitmap.Height * scale);
            int xwidth = (int)(bitmap.Width * scale);
            var dimensions = xwidth * xheight;
            var dots = new BitArray(dimensions);

            for (var y = 0; y < xheight; y++)
            {
                for (var x = 0; x < xwidth; x++)
                {
                    var _x = (int)(x / scale);
                    var _y = (int)(y / scale);
                    var color = bitmap.GetPixel(_x, _y);
                    var luminance = (int)(color.R * 0.3 + color.G * 0.59 + color.B * 0.11);
                    dots[index] = (luminance < threshold);
                    index++;
                }
            }

            return new BitmapData()
            {
                Dots = dots,
                Height = (int)(bitmap.Height * scale),
                Width = (int)(bitmap.Width * scale)
            };
        }
    }

    public class BitmapData
    {
        public BitArray Dots
        {
            get;
            set;
        }

        public int Height
        {
            get;
            set;
        }

        public int Width
        {
            get;
            set;
        }
    }

将函数调用为时发生错误:
byte[] _buffer = PrintImage(FOTO);

错误:

  

“无法从typeref(程序集'System.Drawing.Common,版本= 4.0.1.0,区域性=中性,PublicKeyToken = cc7b13ffcd2ddd51'中的预期类'System.Drawing.Bitmap'中解析带有令牌01000119的类型”) >

0 个答案:

没有答案