|
 |
aspx thread: Question on ASPToday Image Resizing Article
Message #1 by info@e... on Wed, 17 Apr 2002 09:13:48
|
|
Hello All,
I need a function to resize my pictures on the fly and make them
thumbnails. I tried the example article on ASPToday and modified it for
C#, since it was on VB. I finally got my code to compile but when its time
to resize, it throws out a bunch of junk to the browser, unreadable
characters. I would appreciate it if someone can take a look at the code
and tell me whats wrong, or perhaps give me a better function. Thanks!
public void resizeImage()
{
// initialise objects
String strFilename;
System.Drawing.Image g;
System.Drawing.Bitmap g2;
int newWidth, newHeight, sizer;
int boxWidth=100;
int boxHeight=100;
// set the filename
strFilename = Server.MapPath("../images/mylargepix.jpg");
// create a new image from file
g = System.Drawing.Image.FromFile(strFilename);
if (g.Height > g.Width) // portrait
{
sizer = boxWidth / g.Height;
}
else
{
sizer = boxHeight / g.Width;
}
newWidth = Convert.ToInt32(g.Width * sizer);
newHeight = Convert.ToInt32(g.Height * sizer);
g2 = new System.Drawing.Bitmap(g, newWidth,newHeight);
// set the content type
Response.ContentType ="image/jpeg";
// send the image to the viewer
g2.Save(Response.OutputStream, g.RawFormat);
// tidy up
g.Dispose();
}
Thanks!
- Elmer M.
Message #2 by ccoley@b... on Wed, 17 Apr 2002 18:52:06
|
|
Check out http://www.asp.net/ControlGallery/default.aspx?
Category=8&tabindex=2
Hope this helps.
> Hello All,
> I need a function to resize my pictures on the fly and make them
t> humbnails. I tried the example article on ASPToday and modified it for
C> #, since it was on VB. I finally got my code to compile but when its
time
t> o resize, it throws out a bunch of junk to the browser, unreadable
c> haracters. I would appreciate it if someone can take a look at the
code
a> nd tell me whats wrong, or perhaps give me a better function. Thanks!
> public void resizeImage()
> {
> // initialise objects
> String strFilename;
> System.Drawing.Image g;
> System.Drawing.Bitmap g2;
> int newWidth, newHeight, sizer;
> int boxWidth=100;
> int boxHeight=100;
>
> // set the filename
> strFilename = Server.MapPath("../images/mylargepix.jpg");
>
> // create a new image from file
> g = System.Drawing.Image.FromFile(strFilename);
>
> if (g.Height > g.Width) // portrait
> {
> sizer = boxWidth / g.Height;
> }
> else
> {
> sizer = boxHeight / g.Width;
> }
>
> newWidth = Convert.ToInt32(g.Width * sizer);
> newHeight = Convert.ToInt32(g.Height * sizer);
>
> g2 = new System.Drawing.Bitmap(g, newWidth,newHeight);
>
> // set the content type
> Response.ContentType ="image/jpeg";
>
> // send the image to the viewer
> g2.Save(Response.OutputStream, g.RawFormat);
>
> // tidy up
> g.Dispose();
> }
>
> Thanks!
> - Elmer M.
Message #3 by info@e... on Thu, 18 Apr 2002 02:49:24
|
|
Thanks but that's actually not what I am looking for. I want to use my own
function and be able to customize it completely. I do not want to create a
photo album and then show the full image. I also don;t need to read from a
directory, I would pass the location of the image as a parameter and my
function would generate the thumbnail, one at a time. I want to generate
the image on the fly, without saving it.
- Elmer M.
> Check out http://www.asp.net/ControlGallery/default.aspx?
C> ategory=8&tabindex=2
> Hope this helps.
>
>> Hello All,
> > I need a function to resize my pictures on the fly and make them
t> > humbnails. I tried the example article on ASPToday and modified it
for
C> > #, since it was on VB. I finally got my code to compile but when its
t> ime
t> > o resize, it throws out a bunch of junk to the browser, unreadable
c> > haracters. I would appreciate it if someone can take a look at the
c> ode
a> > nd tell me whats wrong, or perhaps give me a better function. Thanks!
> > public void resizeImage()
> > {
> > // initialise objects
> > String strFilename;
> > System.Drawing.Image g;
> > System.Drawing.Bitmap g2;
> > int newWidth, newHeight, sizer;
> > int boxWidth=100;
> > int boxHeight=100;
> >
> > // set the filename
> > strFilename = Server.MapPath("../images/mylargepix.jpg");
> >
> > // create a new image from file
> > g = System.Drawing.Image.FromFile(strFilename);
> >
> > if (g.Height > g.Width) // portrait
> > {
> > sizer = boxWidth / g.Height;
> > }
> > else
> > {
> > sizer = boxHeight / g.Width;
> > }
> >
> > newWidth = Convert.ToInt32(g.Width * sizer);
> > newHeight = Convert.ToInt32(g.Height * sizer);
> >
> > g2 = new System.Drawing.Bitmap(g, newWidth,newHeight);
> >
> > // set the content type
> > Response.ContentType ="image/jpeg";
> >
> > // send the image to the viewer
> > g2.Save(Response.OutputStream, g.RawFormat);
> >
> > // tidy up
> > g.Dispose();
> > }
> >
> > Thanks!
> > - Elmer M.
Message #4 by "Goh Mingkun" <mangokun@h...> on Thu, 18 Apr 2002 03:12:01
|
|
I came across this acticle last year when I was working on my final year
project, and it has proved to be very 'useful' to me, with respect to
creating thumbnail of images.
ASP.NET images part 3: Thumbnails
http://www.aspalliance.com/chrisg/default.asp?article=21
by Chris Garrett
______________________________
IMPORTANT
The source given by this article is not fully complete.
QUOTE from the article above
The "NewThumbSize" function checks to see if the image is portrait or
landscape, then works out the multiplier (the division of our arbitrary
200 pixels and the height or width). There is an obvious flaw here that
the image could end up bigger than it started, I will leave this as an
exercise for the reader ;O)
______________________________
I end up creating a new function based on the author's source code.
And took quite a long time, before figuring out the code needed to
complete my new image resizing function.
Until recently, I need this function in my work.
And my supervisor suggested overloading the function, and putting it in a
Class Library (DLL), so anytime we want to use it, just have to add a
reference to it.
Thus I slightly modified my function, and added a few more overloaded
versions of it.
But basically, overloading means accepting a different set of parameter
with same result output.
______________________________
Below is the full set of my image resizing functions:
#Region "Proportional Image Resizing Functions"
' This function returns a proportional size for thumbnail image
Public Function ScaledThumbnailSize(ByVal ImageWidth As Integer, ByVal
ImageHeight As Integer, ByVal MaxWidth As Integer, ByVal MaxHeight As
Integer) As Size
Dim NewSize As New Size()
If MaxWidth >= ImageWidth And MaxHeight >= ImageHeight Then
' no need to make thumbnail
' as current image already smaller than required size
NewSize.Width = ImageWidth
NewSize.Height = ImageHeight
Else
' Calculate the Size of the New image
Dim tempMultiplier As Double
If ImageWidth = ImageHeight Then ' square image
If MaxWidth <= MaxHeight Then
tempMultiplier = MaxWidth / ImageWidth
Else
tempMultiplier = MaxHeight / ImageWidth
End If
Else ' landscape/portrait
image
If MaxWidth = MaxHeight Then
If ImageWidth > ImageHeight Then ' landscape
tempMultiplier = MaxWidth / ImageWidth
Else ' portrait
tempMultiplier = MaxHeight / ImageHeight
End If
Else
If MaxWidth < ImageWidth And MaxHeight < ImageHeight
Then
' thumbnail area totally inside image area
' try using ImageWidth
tempMultiplier = MaxWidth / ImageWidth
If CInt(ImageHeight * tempMultiplier) > MaxHeight
Then
tempMultiplier = MaxHeight / ImageHeight
End If
ElseIf MaxWidth > ImageWidth Then
' thumbnail area longer than image area
tempMultiplier = MaxHeight / ImageHeight
Else
' thumbnail area taller than image area
tempMultiplier = MaxWidth / ImageWidth
End If
End If
End If
NewSize.Width = CInt(ImageWidth * tempMultiplier)
NewSize.Height = CInt(ImageHeight * tempMultiplier)
End If
Return NewSize
End Function
' This overloaded function takes in a different set of parameters
Public Function ScaledThumbnailSize(ByVal ImageSize As Size, ByVal
MaxSize As Size) As Size
Return ScaledThumbnailSize(ImageSize.Width, ImageSize.Height,
MaxSize.Width, MaxSize.Height)
End Function
' This function would take in an image, and returns a proportional
resized thumbnail image
' The 1st parameter OrginalImage would be the image you want to be
scaled to fit within a container
' The 2nd parameter Container can be an control such as PictureBox, or
an structure such as Rectangle
Public Function ScaledThumbnailImage(ByVal OriginalImage As Image,
ByVal Container As Object) As Image
Dim NewSize As New Size()
NewSize = ScaledThumbnailSize(OriginalImage.Size, Container.Size)
Return OriginalImage.GetThumbnailImage(NewSize.Width,
NewSize.Height, Nothing, Nothing)
End Function
' This overloaded function takes in a different set of parameters
Public Function ScaledThumbnailImage(ByVal OriginalImage As Image,
ByVal ContainerSize As Size) As Image
Dim NewSize As New Size()
NewSize = ScaledThumbnailSize(OriginalImage.Size, ContainerSize)
Return OriginalImage.GetThumbnailImage(NewSize.Width,
NewSize.Height, Nothing, Nothing)
End Function
' This overloaded function takes in a different set of parameters
Public Function ScaledThumbnailImage(ByVal OriginalImage As Image,
ByVal ContainerWidth As Integer, ByVal ContainerHeight As Integer) As Image
Dim NewSize As New Size()
NewSize = ScaledThumbnailSize(OriginalImage.Size.Width,
OriginalImage.Size.Height, ContainerWidth, ContainerHeight)
Return OriginalImage.GetThumbnailImage(NewSize.Width,
NewSize.Height, Nothing, Nothing)
End Function
#End Region
Here are some example usage:
1. Dim imgPhoto As Image = Image.FromFile(OpenFileDialog1.FileName)
PictureBox1.Image = ScaledThumbnailImage(imgPhoto, PictureBox1)
2. Dim img As Image = GetHttpImage(HttpStockChartImageUri)
img = ScaledThumbnailImage(img, 300, 200)
Return img
3. imgPhoto = Image.FromFile(OpenFileDialog1.FileName)
Dim thumbSize As New Size()
thumbSize = ScaledThumbnailSize(imgPhoto.Size, PictureBox1.Size)
PictureBox1.Image = imgPhoto.GetThumbnailImage(thumbSize.Width,
thumbSize.Height, Nothing, Nothing)
Message #5 by "Goh Mingkun" <mangokun@h...> on Thu, 18 Apr 2002 03:24:16
|
|
Note that you MAY HAVE to add 2 namespace to the top of your code, which
are:
Imports System.Drawing
Imports System.Drawing.Imaging
I wanted to share my function long ago, but didn't have the time.
As I prefer VB over C++ or C#, most/all of my code are in VB.
Feel free to convert them to your prefered language.
You can put my 'Proportional Image Resizing Functions' into a Module file
or a Class Library, or even build it into a reusable DLL (althrough the
later approach is typically prefered).
|
|
 |