簡體   English   中英

圖像變量為byte []數組

[英]Image variable to byte[] array

我遇到了一個我認為很難解決的問題,只是我做錯了,所以:我有一個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;

圖片框完美地顯示了圖像,但我無法寫入字節數組。 我嘗試了以下代碼:

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(“Closed file”)。

我的代碼出了什么問題? 謝謝你的幫助! :)

OFF:對不起我的壞ENG ...

編輯:錯誤:

例外:拋出:“無法訪問封閉的流。” (System.ObjectDisposedException)拋出System.ObjectDisposedException:“無法訪問已關閉的Stream。” 時間:2014.08.11。 14:37:49主題:主線程[7276]

例外:拋出:“通用錯誤:GDI +。” (System.Runtime.InteropServices.ExternalException)拋出了System.Runtime.InteropServices.ExternalException:“通用錯誤:GDI +。” 時間:2014.08.11。 14:37:49主題:主線程[7276]

將您的代碼更改為:

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

感謝幫助過我的人,我通過這段代碼弄明白了:

    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