Creating and saving image from byte[]
I am storing an array of bytes into SQL Server. I want to create and save an image from a byte[] but several attempts seem unreliable and result in intermittent exceptions.
What is the best way to go about this?
EDIT: I got exceptions such as incorrect parameter and GDI exception. I don't have 开发者_运维问答the code as this is on my machine at work.
Thanks
Have you tried:
MemoryStream ms = new MemoryStream(byteArray);
Image image = Image.FromStream(ms);
image.Save("myfile.jpg", ImageFormat.Jpg);
If you get exceptions it is likely because your byte[]
is in the wrong format or is corrupted/damaged.
If you mean a bitmap image, then you can create a MemoryStream from the byte array and then load the image from it. As long as the data contains valid image data that .net can decode, you shouldn't get any exceptions - so if you're doing this and getting intermittent exceptions, it's possible that the data is being corrupted or truncated during its trip to/from the database - make sure you're getting back identical data to what you attempted to store.
Otherwise, I'd suggest posting details of the exceptions so we can help you diagnose the issue.
精彩评论