Solved

Resizing an image(BLOB)

Posted on 2004-10-13
5
429 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Validating textboxes in red square borders. 2 43
Securing WEBAPI on Azure 2 54
Using sample Autorize.net c# simple example 1 52
aspx ascx, c# 7 39
I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

738 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