Solved

Resizing an image(BLOB)

Posted on 2004-10-13
5
420 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
5 Comments
 
LVL 25

Expert Comment

by:nauman_ahmed
Comment Utility
0
 

Author Comment

by:ab0u110a
Comment Utility
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
Comment Utility
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
Comment Utility
The question has been PAQ'd and the 400 points have been refunded.
RomMod
Community Support Moderator
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

763 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

Need Help in Real-Time?

Connect with top rated Experts

5 Experts available now in Live!

Get 1:1 Help Now