【问题标题】:Image variable to byte[] array图像变量到 byte[] 数组
【发布时间】:2014-10-04 06:58:26
【问题描述】:

我遇到了一个我认为很容易解决的问题,只是我做错了,所以: 我有一个 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);

我有两个Image 变量:

Image WORKER_sign;
Image EMPLOYER_sign;

我在这些中加载图像,也在图片框中:

    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;

picturebox 完美地显示了图像,但是我不能写入字节数组。我尝试了以下代码:

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);

但最后一行向我抛出了一个 Generic GDI+ 异常,在此之前 IntelliTrace 也显示了一些 System.ObjectDisposedException(“已关闭文件”)。

我的代码有什么问题? 谢谢大家的帮助! :)

OFF:对不起,我的英语不好......

编辑:错误:

异常:抛出:“无法访问已关闭的流。” (System.ObjectDisposedException) 一个 System.ObjectDisposedException 是 抛出:“无法访问封闭的流。”时间:2014.08.11。 14:37:49 线程:主线程[7276]

异常:抛出:“一般错误:GDI+。” (System.Runtime.InteropServices.ExternalException) 一个 System.Runtime.InteropServices.ExternalException 被抛出:“通用 错误:GDI+。”时间:2014.08.11. 14:37:49 线程:主线程[7276]

【问题讨论】:

    标签: c# image winforms bytearray


    【解决方案1】:

    将您的代码更改为:

    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;
    }
    

    【讨论】:

    • img.Save(stream, img.RawFormat); 抛出相同的异常 :( 但谢谢 :)
    • @AtanasDesev 问题不在于您发布的代码,而是用于加载图像的流已关闭。 Image.FromStream 拥有流的所有权,并且不能关闭流。
    • @Dirk 谢谢,刚刚在更新帖中看到了异常。
    【解决方案2】:

    感谢所有帮助我的人,我通过这段代码弄清楚了事情:

        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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-01
      • 1970-01-01
      • 2012-04-06
      • 2021-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-10
      相关资源
      最近更新 更多