?
Solved

Uploading Image to db and resizing

Posted on 2004-04-08
6
Medium Priority
?
369 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
[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
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

777 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