-
Jan 23rd, 2006, 10:23 AM
#1
Thread Starter
Frenzied Member
High Quality Image Resizing
By changing interpolation mode (the algorithm used to resize) on a graphics object, you can resize your images and keep their quality.
It will use more cpu time then usual, that's why "nearest neighbour" is the traditionally used algorithm, as it is a low cpu cost method.
Here's is a function to resize your Images at a high quality
Code:
Private Function SizeImage(ByVal img As Bitmap, ByVal width As Integer, ByVal height As Integer) As Bitmap
Dim newBit As New Bitmap(width, Height) 'new blank bitmap
Dim g As Graphics = Graphics.FromImage(newBit)
'change interpolation for reduction quality
g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
g.DrawImage(img, 0, 0, width, Height)
Return newBit
End Function
Last edited by Phill64; Dec 17th, 2007 at 03:29 PM.
-
Jan 28th, 2006, 09:49 AM
#2
Fanatic Member
-
Feb 24th, 2006, 09:30 AM
#3
New Member
Re: High Quality Image Resizing
Can you do it also with an image? in use of a bitmap
-
Feb 24th, 2006, 09:34 AM
#4
Thread Starter
Frenzied Member
Re: High Quality Image Resizing
Bitmap is an inheritance of Image, so all you need to do is modify the header to input&output of type Image
Code:
Private Function SizeImage(ByVal img As Image, ByVal width As Integer, ByVal height As Integer) As Image
Last edited by Phill64; Dec 17th, 2007 at 03:32 PM.
-
Feb 28th, 2006, 09:52 AM
#5
Member
Re: High Quality Image Resizing
Can I take an image from the server and change the dimension with this function?
-
Feb 28th, 2006, 10:16 AM
#6
Member
Re: High Quality Image Resizing
The function does not change the image dimension. I get no errors. Probably using the function wrong. Any ideas?
VB Code:
Sub UploadImage()
Dim url As String = "/images/artikkelBilder/"
Dim bildeTeller As Integer = 0
If Not (fileupload1.PostedFile Is Nothing) Then 'Is there something to upload?
Dim strLongFilePath As String = fileupload1.PostedFile.FileName
Dim intFileNameLength As Integer = InStr(1, StrReverse(strLongFilePath), "\")
Dim strFileName As String
If intFileNameLength > 0 Then
strFileName = Mid(strLongFilePath, (Len(strLongFilePath) - intFileNameLength) + 2)
Else
strFileName = strLongFilePath
End If
Select Case fileupload1.PostedFile.ContentType
Case "image/pjpeg", "image/jpeg", "image/png", "image/gif" 'Valid file?
fileupload1.PostedFile.SaveAs(Server.MapPath(url) & strFileName)
Dim bilde As Image = Image.FromFile(Server.MapPath(url) & strFileName)
SizeImage(bilde, 150, 100)
lbStatus.Text += "Image path: " & "http://www.dyrebeskyttelsenhordaland.ord/images/artikkelBilder/" & strFileName & "<br>"
Case Else
'Not valid file
lbStatus.Text += "Ugyldig bildeformat! <br>"
End Select
End If
End Sub
Private Function SizeImage(ByVal img As Image, ByVal width As Integer, ByVal height As Integer) As Image
Dim newBit As New Bitmap(width, height) 'new blank bitmap
Dim g As Graphics = Graphics.FromImage(newBit)
'change interpolation for reduction quality
g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
g.DrawImage(img, 0, 0, width, height)
Return newBit
End Function
End Class
-
Feb 28th, 2006, 10:23 AM
#7
Thread Starter
Frenzied Member
Re: High Quality Image Resizing
I think you were under the assumption it is a sub which takes an object reference and simply changes your image. Infact it is a functon which accept your image & dimension and RETURNS a new image.
so you should change this line
VB Code:
SizeImage(bilde, 150, 100)
To
VB Code:
bilde = SizeImage(bilde, 150, 100)
-
Feb 28th, 2006, 11:26 AM
#8
Member
Re: High Quality Image Resizing
Thanks for quick reply.
It did'nt work, no difference.
After it returns the new image, i'm supposed to save it again on the server?
-
Feb 28th, 2006, 11:31 AM
#9
Thread Starter
Frenzied Member
Re: High Quality Image Resizing
if you want it changed on the file, yes.
You will have to dispose the original image to do this otherwise you will get access errors
Code:
Dim temp As Bitmap = SizeImage(bilde, 150, 100)
bilde.dispose
bilde = temp
bilde.Save(filename)
Last edited by Phill64; Dec 17th, 2007 at 03:31 PM.
-
Feb 28th, 2006, 11:36 AM
#10
Member
Re: High Quality Image Resizing
Thanks Phill64, worked like a charm
-
Mar 1st, 2006, 07:04 AM
#11
Member
Re: High Quality Image Resizing
One more question
I want the last added picture to be deleted if the user uploads a new one.
Looks like it does'nt go through the IF, here is the code:
http://pastebin.com/578210
Solved it, had to use try catch and session
Last edited by Spanjis; Mar 2nd, 2006 at 06:11 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|