?
Solved

Resizing an image(BLOB)

Posted on 2004-10-13
5
Medium Priority
?
435 Views
Last Modified: 2008-03-17
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.
0
Comment
Question by:ab0u110a
  • 2
4 Comments
 

Author Comment

by:ab0u110a
ID: 12306242
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
0
 

Author Comment

by:ab0u110a
ID: 12306818
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
                        {
                              
                        }
0
 

Accepted Solution

by:
RomMod earned 0 total points
ID: 12339492
The question has been PAQ'd and the 400 points have been refunded.
RomMod
Community Support Moderator
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Integration Management Part 2
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month17 days, 8 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question