Solved

Resizing an image(BLOB)

Posted on 2004-10-13
5
428 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
ID: 12299575
0
 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
asp.net input html code have it display the content 2 30
Iteration Help (Asp.net VB) 5 35
How to repeat the data 4 30
Upgrade code from VS 2010 to VS 2015 7 31
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

685 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