Solved

Uploading Image to db and resizing

Posted on 2004-04-08
6
366 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 12

Accepted Solution

by:
dfiala13 earned 500 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

828 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