我遇到了一个我觉得很难解决的问题,只是我做错了,所以: 我有一个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是 抛出:“无法访问封闭的流。”时间:2014.08.11。 14时37分49秒 线程:主线程[7276]
异常:抛出:“通用错误:GDI +。” (System.Runtime.InteropServices.ExternalException)A 抛出了System.Runtime.InteropServices.ExternalException:“Generic 错误:GDI +。“时间:2014.08.11.14:37:49线程:主线程[7276]
答案 0 :(得分:3)
将您的代码更改为:
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;
}
答案 1 :(得分:0)
感谢所有帮助过我的人,我通过这段代码弄清楚了这些事情:
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;