Groups | Blog | Home
all groups > dotnet drawing api > september 2005 >

dotnet drawing api : invalid bitmap???



Morten Wennevik
9/26/2005 12:00:00 AM
Hi Abraham,

The SQL Image type adds a header to the bitmap data, which if my memory serves me right is exactly 80 bytes. You need to remove these 80 bytes before creating a Bitmap out of the data.

--
Happy coding!
Abraham Andres Luna
9/26/2005 11:07:36 AM
i save a compressed bitmap to an image field in my sql database:

ImageCodecInfo iciJPEG;
foreach (ImageCodecInfo cdc in ImageCodecInfo.GetImageEncoders())
{
if (cdc.MimeType == "image/jpeg")
{
iciJPEG = cdc;
}
}
EncoderParameters epsQuality = new EncoderParameters(1);
long lngQuality = 90;
epsQuality.Param[0] = new
EncoderParameter(System.Drawing.Imaging.Encoder.Quality, lngQuality);
SqlConnection connRDK = new
SqlConnection(ConfigurationManager.ConnectionStrings["TestConnection"].ToString());
connRDK.Open();
SqlDataAdapter daRDK = new SqlDataAdapter("SELECT Picture FROM VHVINPIC
WHERE ItmId = '15336'", connRDK);
DataTable dtPicture= new DataTable();
daRDK.Fill(dtPicture);
DataRow drPicture = dtPicture.Rows[0];
MemoryStream msImage = new MemoryStream((byte[])drPicture["Picture"]);
Bitmap bmpImage = new Bitmap(msImage);
MemoryStream msImageOpt = new MemoryStream();
bmpImage.Save(Server.MapPath("images/pic15336_opt.jpg"), iciJPEG,
epsQuality); //this line works, i was able to open the bitmap
bmpImage.Save(msImageOpt, iciJPEG, epsQuality); //i save it to a memory
stream so i can read the bytes to insert into sql
BinaryReader br = new BinaryReader(msImageOpt); //setup reader
byte[] bytePicture = br.ReadBytes((int)msImageOpt.Length); /read bytes
SqlCommand commRDK = new SqlCommand("UPDATE VHVINPIC SET Picture =
@Picture WHERE ItmId = '15336'", connRDK);
commRDK.Parameters.Add("@Picture", SqlDbType.Image,
bytePicture.Length).Value = bytePicture;
commRDK.ExecuteNonQuery();
daRDK.Dispose();
connRDK.Close();
connRDK.Dispose();


when i try to pull down the compressed image from sql it throws an error:

string strItmId = "15336";
SqlConnection connRDK = new
SqlConnection(ConfigurationManager.ConnectionStrings["TestConnection"].ToString());
SqlDataAdapter daRDK = new SqlDataAdapter("SELECT Picture FROM VHVINPIC
WHERE ItmId = '" + strItmId + "'", connRDK);
DataTable dtItmIds = new DataTable();
daRDK.Fill(dtItmIds);
daRDK.Dispose();
connRDK.Close();
connRDK.Dispose();
DataRow drPicture = dtItmIds.Rows[0];
MemoryStream msImage = new MemoryStream((byte[])drPicture["Picture"]);
Bitmap bmpImage = new Bitmap(msImage); //throws error at this line
bmpImage.Save(Server.MapPath("images/pic" + strItmId + ".jpg"));
if (bmpImage.HorizontalResolution > 72)
{
bmpImage.SetResolution(72, 72);
bmpImage.Save(Server.MapPath("images/pic" + strItmId + "_72dpi.jpg"));
}


the error is :
System.ArgumentException: Parameter is not valid.


i guess i'm not saving it to sql correctly cause it compressed and saved the
bitmap to the disk fine

thank you for your help

Abraham Andres Luna
9/26/2005 2:59:46 PM
thank you for the answer, i decided to use SqlDbType.Binary instead so it
wont add the header info, now it works fine

[quoted text, click to view]

AddThis Social Bookmark Button