11

I encounter this error when I'm trying to save my image into database.

What am I doing wrong? enter image description here

Here's the design of my table:

enter image description here

I'm using Microsoft Server 2008.

H H
  • 263,252
  • 30
  • 330
  • 514
yonan2236
  • 13,371
  • 33
  • 95
  • 141

2 Answers2

15

You have two issues:

  1. The (understandable) confusion about the Image data type in SQL Server. This is actually just a large binary object (a BLOB in common parlance). In order to save an image (or anything else) in this column, you have to first convert it to a byte[], then store that byte array in the column.
  2. You're using the Image data type, which is deprecated. If you have control over this design, change it to use varbinary(MAX). While the Image type is still in SQL Server 2008 R2, it will be removed from future versions at some point.

To get a byte[] representing the image, try this out:

byte[] data;

using(System.IO.MemoryStream stream = new System.IO.MemoryStream())
{
    image.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
    data = stream.ToArray();
}

The data variable now contains the binary data of the image, and you can use that as your parameter value. There are additional steps you can take here (saving it in another format like JPEG, for example), but this should at least get you started.

When retrieving the data, it'll also come back as a byte[], so you'll need to turn that into an image again.

byte[] data = ...;

Image image = Image.FromStream(new System.IO.MemoryStream(data));
Adam Robinson
  • 182,639
  • 35
  • 285
  • 343
  • 1
    @yonan2236: Can you be more specific...? – Adam Robinson Jan 31 '11 at 15:35
  • i got an error for this code: `image.Save(stream)` - has some invalid arguments – yonan2236 Jan 31 '11 at 15:35
  • i got again another error: `String or binary data would be truncated. The statement has been terminated` Sorry, I can't make it work. My data type in my database server for image is varbinary(MAX). Right? – yonan2236 Jan 31 '11 at 15:39
  • @yonan2236: Yes, `varbinary(MAX)`. Are you passing in values for `ProductId` or `FileName` that are larger than the fields? Both have a maximum length of 50, and I could easily see a file name being more than 50 characters. – Adam Robinson Jan 31 '11 at 15:44
  • yes! it's the filename causing that error : ) Thank you very much sir... Just one last question... When it comes to retrieval, do i have to convert the varbinary from server into image? so i can dispaly it in my application right? – yonan2236 Jan 31 '11 at 15:56
  • @yonan2236: I'll post an edit that does that in just a moment. – Adam Robinson Jan 31 '11 at 16:06
  • Wish i can give another credit to you for that. Thanks again sir. :) – yonan2236 Feb 01 '11 at 09:35
0

The error is pretty clear, there is no conversion from a GDI Image object to a byte array (which is what the SQL Server data column type Image really is). You need to convert your Image object into a byte array before adding it as a parameter.

Otávio Décio
  • 73,752
  • 17
  • 161
  • 228