ab0u110a
asked on
Resizing an image(BLOB)
My colleague just asked a question about displaying blobs. Now that is sorted it is up to me to do the graphic however i was wondering if anyone knew if there was a way to resize a blob image or resize the image before converting it to a blob i.e before its stored in the database.
Is there a way to resize an image without saving it on the server or clients computer then turning into a blob.
Ideally i would like to be able to
1) let the user choose the file,
2) click upload
3) the image is resized and then turned into a blob and stored in the database
I use a simple upload script
if (Page.IsValid) //save the image
{
Stream imgStream = File1.PostedFile.InputStre am;
int imgLen = File1.PostedFile.ContentLe ngth;
string imgContentType = File1.PostedFile.ContentTy pe;
byte[] imgBinaryData = new byte[imgLen];
int n = imgStream.Read(imgBinaryDa ta,0,imgLe n);
int RowsAffected = SaveToDB(imgBinaryData,img ContentTyp e);
if ( RowsAffected>0 )
{
}
else
{
}
}
and convert to a blob using this code
//use the web.config to store the connection string
NameValueCollection col1 = ConfigurationSettings.AppS ettings;
string strConnection1 = col1.Get("sqlConnection1.C onnectionS tring");
SqlConnection SqlConnection2 = new SqlConnection(strConnectio n1);
string imgName = NameID + "photo";
SqlConnection2.Open();
SqlCommand command = new SqlCommand( "Update tblUsers Set UserPhotoName = @img_name, UserPhoto = @img_data, UserPhotoType = @img_contenttype where ID=" + "'" + IDD + "'", SqlConnection2 );
SqlParameter param0 = new SqlParameter( "@img_name", SqlDbType.VarChar,50 );
param0.Value = imgName;
command.Parameters.Add( param0 );
SqlParameter param1 = new SqlParameter( "@img_data", SqlDbType.Image );
param1.Value = imgbin;
command.Parameters.Add( param1 );
SqlParameter param2 = new SqlParameter( "@img_contenttype", SqlDbType.VarChar,50 );
param2.Value = imgcontenttype;
command.Parameters.Add( param2 );
int numRowsAffected = command.ExecuteNonQuery();
SqlConnection2.Close();
return numRowsAffected;
If anyone can think please help.
Is there a way to resize an image without saving it on the server or clients computer then turning into a blob.
Ideally i would like to be able to
1) let the user choose the file,
2) click upload
3) the image is resized and then turned into a blob and stored in the database
I use a simple upload script
if (Page.IsValid) //save the image
{
Stream imgStream = File1.PostedFile.InputStre
int imgLen = File1.PostedFile.ContentLe
string imgContentType = File1.PostedFile.ContentTy
byte[] imgBinaryData = new byte[imgLen];
int n = imgStream.Read(imgBinaryDa
int RowsAffected = SaveToDB(imgBinaryData,img
if ( RowsAffected>0 )
{
}
else
{
}
}
and convert to a blob using this code
//use the web.config to store the connection string
NameValueCollection col1 = ConfigurationSettings.AppS
string strConnection1 = col1.Get("sqlConnection1.C
SqlConnection SqlConnection2 = new SqlConnection(strConnectio
string imgName = NameID + "photo";
SqlConnection2.Open();
SqlCommand command = new SqlCommand( "Update tblUsers Set UserPhotoName = @img_name, UserPhoto = @img_data, UserPhotoType = @img_contenttype where ID=" + "'" + IDD + "'", SqlConnection2 );
SqlParameter param0 = new SqlParameter( "@img_name", SqlDbType.VarChar,50 );
param0.Value = imgName;
command.Parameters.Add( param0 );
SqlParameter param1 = new SqlParameter( "@img_data", SqlDbType.Image );
param1.Value = imgbin;
command.Parameters.Add( param1 );
SqlParameter param2 = new SqlParameter( "@img_contenttype", SqlDbType.VarChar,50 );
param2.Value = imgcontenttype;
command.Parameters.Add( param2 );
int numRowsAffected = command.ExecuteNonQuery();
SqlConnection2.Close();
return numRowsAffected;
If anyone can think please help.
ASKER
Hi thanks for the link s but they all involvve uploading the image to the server first i want to read the image into a stream and resize it from there if possible
Thanks
Thanks
ASKER
Hi thanks i have found a way to do it quite quickly and successfully
if (Page.IsValid) //save the image
{
Stream imgStream1 = File1.PostedFile.InputStre am;
System.Drawing.Image image = System.Drawing.Image.FromS tream(imgS tream1);
System.Drawing.Image thumbnailImage = image.GetThumbnailImage(13 3, 200, new System.Drawing.Image.GetTh umbnailIma geAbort(Th umbnailCal lback), IntPtr.Zero);
MemoryStream imageStream = new MemoryStream();
thumbnailImage.Save(imageS tream, System.Drawing.Imaging.Ima geFormat.J peg);
byte[] imageContent = new Byte[imageStream.Length];
imageStream.Position = 0;
int imgLen = Convert.ToInt32(imageStrea m.Length);
string imgContentType = File1.PostedFile.ContentTy pe;
int n = imageStream.Read(imageCont ent,0,imgL en);
int RowsAffected = SaveToDB(imageContent,imgC ontentType );
if ( RowsAffected>0 )
{
ChangeImage.Visible = false;
}
else
{
}
if (Page.IsValid) //save the image
{
Stream imgStream1 = File1.PostedFile.InputStre
System.Drawing.Image image = System.Drawing.Image.FromS
System.Drawing.Image thumbnailImage = image.GetThumbnailImage(13
MemoryStream imageStream = new MemoryStream();
thumbnailImage.Save(imageS
byte[] imageContent = new Byte[imageStream.Length];
imageStream.Position = 0;
int imgLen = Convert.ToInt32(imageStrea
string imgContentType = File1.PostedFile.ContentTy
int n = imageStream.Read(imageCont
int RowsAffected = SaveToDB(imageContent,imgC
if ( RowsAffected>0 )
{
ChangeImage.Visible = false;
}
else
{
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
www.codeproject.com/aspnet/thumbtools.asp
http://www.codetoad.com/asp.net_thumbnails.asp
http://weblogs.asp.net/jrule/archive/2004/08/16/215393.aspx
www.codeproject.com/aspnet/ DynamicASPDotNETTextImage.
Best, Nauman.