Reputation: 124
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.
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 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.
What's wrong with my code? Thank you for all the help! :)
OFF: Sorry for my bad ENG...
EDIT: The errors:
Exception:Thrown: "Cannot access a closed Stream." (System.ObjectDisposedException) A System.ObjectDisposedException was thrown: "Cannot access a closed Stream." Time: 2014.08.11. 14:37:49 Thread:Main Thread[7276]
Exception:Thrown: "Generic error in: GDI+." (System.Runtime.InteropServices.ExternalException) A System.Runtime.InteropServices.ExternalException was thrown: "Generic error in: GDI+." Time: 2014.08.11. 14:37:49 Thread:Main Thread[7276]
Upvotes: 2
Views: 831
Reputation: 124
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;
Upvotes: 0
Reputation: 868
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;
}
Upvotes: 3