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
Re: High Quality Image Resizing
Hi Phill,
This is a really handy code..:) really cool...Im gonna need to be able to zoom and stuff for one of my softwares you know about..Its gonna be quite useful to me atleast and also to tonnes of other people ;)
Keep coding....
Re: High Quality Image Resizing
Can you do it also with an image? in use of a bitmap
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
Re: High Quality Image Resizing
Can I take an image from the server and change the dimension with this function?
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
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)
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?
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)
Re: High Quality Image Resizing
Thanks Phill64, worked like a charm :)
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 :)