?
Solved

Uploading Image to db and resizing

Posted on 2004-04-08
6
Medium Priority
?
371 Views
Last Modified: 2010-04-15
I'm using this code in a page to upload an image to a Sql2000 database.  Everything is working as I have it, but I want to automatically resize the image height and width before it gets uploaded to the db.  I don't really care about file size, just dimensions.  I can't find any way to check the width or height of the file that is being uploaded.  

aspx page:
<input id="browse" type="file" runat="server" NAME="browse"><asp:button id="btnNewPhoto" runat="server" Text="Change Photo" Visible="False" />

code behind:

Stream imgStream = browse.PostedFile.InputStream;
int imgLen = browse.PostedFile.ContentLength;
string imgContentType = browse.PostedFile.ContentType;
byte[] imgBinaryData = new byte[imgLen];
int n = imgStream.Read(imgBinaryData,0,imgLen);

.
.
Lines Omitted for clarity
.
.

SqlParameter parmPhoto = cmd.Parameters.Add("@photo", SqlDbType.Image);
parmPhoto.Value = imgBinaryData;

SqlParameter parmPhotoType = cmd.Parameters.Add("@photoType",SqlDbType.NVarChar, 50);
parmPhotoType.Value = imgContentType;




Thank you for viewing,
dave
0
Comment
Question by:KaiserSose
  • 3
  • 3
6 Comments
 
LVL 12

Expert Comment

by:dfiala13
ID: 10784197
To get the image size load the stream nto an image object

 System.Drawing.Image  img = Image.FromStream(imgStream)
 RectangleF r = GetBounds(GraphicsUnit.Pixel);

float h = r.Height;
float w = r.Width;
0
 
LVL 2

Author Comment

by:KaiserSose
ID: 10786004
That looks like it should work, but the code just doesn't compile.  It doesnt like GetBounds and it throws an 'ambiguoous' error on the Image.FromStream.

dave
0
 
LVL 2

Author Comment

by:KaiserSose
ID: 10786169
I got it to work like this:

System.Drawing.Image img = System.Drawing.Image.FromStream(imgStream);

I don't know why I have to specify System.Drawing beacuse I'm 'using' it at the top of the page.

It still doesn't like the GetBounds.  I can't even get to that in msdn if i use the C# filter.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 12

Accepted Solution

by:
dfiala13 earned 2000 total points
ID: 10786254
Sorry, bad typing, should have just cut and paste.

RectangleF r = img.GetBounds(GraphicsUnit.Pixel);

need to use full namespace because Image belongs to more than one namespace, hence ambigutiy error
0
 
LVL 2

Author Comment

by:KaiserSose
ID: 10786310
I got it working using this:

System.Drawing.Image img = System.Drawing.Image.FromStream(imgStream);
                  int w = img.Width;
                  int h = img.Height;

and an if statement to check for my bounds.  You were close enough though, thanks for the help.
0
 
LVL 12

Expert Comment

by:dfiala13
ID: 10786325
Much slicker.  sorry I was so sloppy on my initial post.
0

Featured Post

Industry Leaders: 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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

807 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