Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to resize image from memorystream to file?

Status
Not open for further replies.

BlackRed

Programmer
Sep 20, 2006
12
GB
Hello,

I have some images stored in a database, but they are all rather large in dimensions (some are 3000px wide!), and I want to resize them and save them to disk the first time they are requested so my website is not pulling out these massive images every time.

I can pull out the images from the database and can display them just fine by modifying the content type of the the page I am writing them out to - but this is no good obviously because they don't resize and they're not saving to disk for future use.

I've got this far following a tutorial...
Code:
Dim imgPhotoVert As System.Drawing.Image = System.Drawing.Image.FromStream(New System.IO.MemoryStream(image.data))
Dim imgPhoto As System.Drawing.Image = Nothing

imgPhoto = FixedSize(imgPhotoVert, Width, Height)
imgPhoto.Save(sbFileName.ToString, System.Drawing.Imaging.ImageFormat.Jpeg)
imgPhoto.Dispose()

Private Function FixedSize(ByVal imgPhoto As System.Drawing.Image, ByVal Width As Integer, ByVal Height As Integer) As System.Drawing.Image
    Dim sourceWidth As Integer = imgPhoto.Width
    Dim sourceHeight As Integer = imgPhoto.Height
    Dim sourceX As Integer = 0
    Dim sourceY As Integer = 0
    Dim destX As Integer = 0
    Dim destY As Integer = 0

    Dim nPercent As Double = 0
    Dim nPercentW As Double = 0
    Dim nPercentH As Double = 0

    nPercentW = Width / sourceWidth
    nPercentH = Height / sourceHeight

    If (nPercentH < nPercentW) Then
        nPercent = nPercentH
        destX = (Width - (sourceWidth * nPercent)) / 2
    Else
        nPercent = nPercentW
        destY = (Height - (sourceHeight * nPercent)) / 2
    End If

    Dim destWidth As Integer = sourceWidth * nPercent
    Dim destHeight As Integer = sourceHeight * nPercent

    Dim bmPhoto As System.Drawing.Bitmap = New Drawing.Bitmap(Width, Height, Drawing.Imaging.PixelFormat.Format24bppRgb)
    bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution)

    Dim grPhoto As System.Drawing.Graphics = Drawing.Graphics.FromImage(bmPhoto)
    grPhoto.Clear(Drawing.Color.Red)
    grPhoto.InterpolationMode = Drawing.Drawing2D.InterpolationMode.HighQualityBicubic

    grPhoto.DrawImage(imgPhoto, New Drawing.Rectangle(destX, destY, destWidth, destHeight), New Drawing.Rectangle(sourceX, sourceY, sourceWidth, sourceHeight), Drawing.GraphicsUnit.Pixel)

    grPhoto.Dispose()
    Return bmPhoto

End Function

But on this line:

Code:
imgPhoto2.Save(sbFileName.ToString, System.Drawing.Imaging.ImageFormat.Jpeg)

I get this error:

A generic error occurred in GDI+.

Obviously that's not the all the code, but it's the part that supposed be resizing and saving images!

Can anyone help me out?

Cheers,
Greg.
 
You know what folks, never mind! My dumb-ass fault; I wasn't server.mappathing my filename, so this didn't work:

imgPhoto.Save(sbFileName.ToString, System.Drawing.Imaging.ImageFormat.Jpeg)

But this does:

imgPhoto2.Save(Server.MapPath(sbFileName.ToString), System.Drawing.Imaging.ImageFormat.Jpeg)

Doh!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top