Solved

Uploading Image to db and resizing

Posted on 2004-04-08
6
363 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 12

Accepted Solution

by:
dfiala13 earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Much slicker.  sorry I was so sloppy on my initial post.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

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

8 Experts available now in Live!

Get 1:1 Help Now