Link to home
Start Free TrialLog in
Avatar of ab0u110a
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.InputStream;
                        int imgLen = File1.PostedFile.ContentLength;
                        string imgContentType = File1.PostedFile.ContentType;
                        
                        byte[] imgBinaryData = new byte[imgLen];
                        int n = imgStream.Read(imgBinaryData,0,imgLen);

                        int RowsAffected = SaveToDB(imgBinaryData,imgContentType);
                        if ( RowsAffected>0 )
                        {
                              
                        }
                        else
                        {
                              
                        }

                  }


and convert to a blob using this code


//use the web.config to store the connection string
                  NameValueCollection col1 = ConfigurationSettings.AppSettings;
                  string strConnection1 = col1.Get("sqlConnection1.ConnectionString");
                  SqlConnection SqlConnection2 = new SqlConnection(strConnection1);
                  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.
Avatar of nauman_ahmed
nauman_ahmed
Flag of United States of America image

Avatar of ab0u110a
ab0u110a

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
Hi thanks i have found a way to do it quite quickly and successfully

if (Page.IsValid) //save the image
                  {
                        Stream imgStream1 = File1.PostedFile.InputStream;

                        System.Drawing.Image image = System.Drawing.Image.FromStream(imgStream1);
                  
                        System.Drawing.Image thumbnailImage = image.GetThumbnailImage(133, 200, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);
            
                        MemoryStream imageStream = new MemoryStream();
                        thumbnailImage.Save(imageStream, System.Drawing.Imaging.ImageFormat.Jpeg);
                        
                        byte[] imageContent = new Byte[imageStream.Length];
      
                        imageStream.Position = 0;

                        int imgLen = Convert.ToInt32(imageStream.Length);
                        string imgContentType = File1.PostedFile.ContentType;
                        
                        int n = imageStream.Read(imageContent,0,imgLen);

                        int RowsAffected = SaveToDB(imageContent,imgContentType);
                        if ( RowsAffected>0 )
                        {
                              ChangeImage.Visible = false;
                        }
                        else
                        {
                              
                        }
ASKER CERTIFIED SOLUTION
Avatar of RomMod
RomMod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial