图像变量到byte[]数组
本文关键字:数组 byte 变量 图像 | 更新日期: 2023-09-27 18:17:22
我面临着一个我认为很容易解决的问题,只是我做错了什么,所以:我有一个Android平板电脑,在那里用户可以绘制签名,我得到图像(. jpeg)由adb.
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 ("Closed file") too.
我的代码有什么问题?谢谢你的帮助!:)
关闭:对不起,我的英文不好…
编辑:错误:
Exception:Thrown: "Cannot access a closed Stream."(System. objectdisposedexception)ObjectDisposedException是"无法访问已关闭的流。"时间:2014.08.11。14:37:49主线程的线程:[7276]
Exception:Thrown: "Generic error in: GDI+."(System.Runtime.InteropServices.ExternalException)System.Runtime.InteropServices.ExternalException被抛出:"Generic错误:GDI+。"时间:2014.08.11。14:37:49 Thread:Main Thread[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;