简体   繁体   English

图像变量为byte []数组

[英]Image variable to byte[] array

I'm faced in a problem which i think is simple to solve, just i did something wrong, so: I have an Android tablet, where the user can draw signature, i get the image (.JPEG) by adb. 我遇到了一个我认为很难解决的问题,只是我做错了,所以:我有一个Android平板电脑,用户可以在其中绘制签名,我通过adb得到图像(.JPEG)。

    ProcessStartInfo adb_copy = new ProcessStartInfo("C:/SCR/adb/adb.exe");
    adb_copy.Arguments = "pull \"mnt/sdcard/sign.jpg\" \"C:\\SCR\\temp\\sign.jpg\"";
    adb_copy.WindowStyle = ProcessWindowStyle.Hidden;
    Process.Start(adb_copy);

I have two Image variables: 我有两个Image变量:

Image WORKER_sign;
Image EMPLOYER_sign;

I load the image in these, and in a picturebox too: 我在这些中加载图像,并在图片框中加载:

    using (FileStream stream = new FileStream("C:/SCR/temp/sign.jpg", FileMode.Open, FileAccess.Read))
    {
        WORKER_sign = Image.FromStream(stream);
        stream.Close();
    }
    pictureBox3.Image = WORKER_sign;
    pictureBox3.SizeMode = PictureBoxSizeMode.Zoom;

The picturebox shows the images perfectly, however i can't write in a byte array. 图片框完美地显示了图像,但我无法写入字节数组。 I tried the following code: 我尝试了以下代码:

public static byte[] ImageToByte(Image img)
{
    if (img == null) return null;
    byte[] result;
    using (MemoryStream stream = new MemoryStream())
    {
        img.Save(stream, img.RawFormat);
        result = stream.GetBuffer();
    }
    return result;
}
byte[] temparray = ImageToByte(WORKER_SIGN);

But the last line throws me a Generic GDI+ exception, and before this the IntelliTrace shows some System.ObjectDisposedException ("Closed file") too. 但最后一行抛出了一个Generic GDI +异常,在此之前,IntelliTrace也显示了一些System.ObjectDisposedException(“Closed file”)。

What's wrong with my code? 我的代码出了什么问题? Thank you for all the help! 谢谢你的帮助! :) :)

OFF: Sorry for my bad ENG... OFF:对不起我的坏ENG ...

EDIT: The errors: 编辑:错误:

Exception:Thrown: "Cannot access a closed Stream." 例外:抛出:“无法访问封闭的流。” (System.ObjectDisposedException) A System.ObjectDisposedException was thrown: "Cannot access a closed Stream." (System.ObjectDisposedException)抛出System.ObjectDisposedException:“无法访问已关闭的Stream。” Time: 2014.08.11. 时间:2014.08.11。 14:37:49 Thread:Main Thread[7276] 14:37:49主题:主线程[7276]

Exception:Thrown: "Generic error in: GDI+." 例外:抛出:“通用错误:GDI +。” (System.Runtime.InteropServices.ExternalException) A System.Runtime.InteropServices.ExternalException was thrown: "Generic error in: GDI+." (System.Runtime.InteropServices.ExternalException)抛出了System.Runtime.InteropServices.ExternalException:“通用错误:GDI +。” Time: 2014.08.11. 时间:2014.08.11。 14:37:49 Thread:Main Thread[7276] 14:37:49主题:主线程[7276]

Change your code to: 将您的代码更改为:

public static byte[] ImageToByte(Image img)
{
    if (img == null) return null;
    byte[] result;
    using (MemoryStream stream = new MemoryStream())
    {
        img.Save(stream, img.RawFormat);
        result = stream.ToArray();
    }
    return result;
}

Thanks for everbody who helped me, I'm figured out the things by this code: 感谢帮助过我的人,我通过这段代码弄明白了:

    EMPLOYER_SIGN = new Bitmap(426, 155);
    using (Graphics gr = Graphics.FromImage(EMPLOYER_SIGN))
    {
        gr.SmoothingMode = SmoothingMode.HighQuality;
        gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
        gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
        gr.DrawImage(Image.FromFile("C:/SCR/temp/sign.jpg"), new Rectangle(0, 0, 426, 155));
    }
    MemoryStream ms = new MemoryStream();
    EMPLOYER_SIGN.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
    BARRAY_EMPSIGN = ms.ToArray();
    ms.Dispose();
    pictureBox3.Image = EMPLOYER_SIGN;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM