Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read SiteMap

Tags: , , , , ,

Sponsored Links



Unable to change the dimension of image in VB.NET

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 11-12-2009
Member
 
Join Date: Jun 2009
Posts: 384
Unable to change the dimension of image in VB.NET

I am unable to change the dimension of an image with the following code:
Code:
Dim pictbox As New PictureBox
Dim img_url As String = Server.MapPath("~/uploads/" & code_url & "/") + System.IO.Path.GetFileName(fup.PostedFile.FileName)
Dim MyStream As FileStream = New FileStream(img_url, FileMode.Open)
pictbox.Image = Drawing.Image.FromStream(MyStream)
pictbox.SizeMode = PictureBoxSizeMode.StretchImage
Dim height As Integer
Dim width As Integer
Dim W As Integer
Dim H As Integer
Dim coef As Double
height = pictbox.Image.Height
width = pictbox.Image.Width
pictbox.Height = H
pictbox.Width = W
pictbox.Update()
Try
    If type_file = ".png" Then
        pictbox.Image.Save(Server.MapPath("~/uploads/" & code_url & "/") + name_vig_file, Imaging.ImageFormat.Png)
    ElseIf type_file = ".jpg" Or type_file = ".jpeg" Then
        pictbox.Image.Save(Server.MapPath("~/uploads/" & code_url & "/") + name_vig_file, Imaging.ImageFormat.Jpeg)
    ElseIf type_file = ".gif" Then
        pictbox.Image.Save(Server.MapPath("~/uploads/" & code_url & "/") + name_vig_file, Imaging.ImageFormat.Gif)
    End If
Catch ex As Exception
    Console.WriteLine(ex.Message)
End Try
MyStream.Close()
Return True
I can not assign a H and W have the image to resize!
Reply With Quote
  #2  
Old 11-12-2009
Member
 
Join Date: Nov 2008
Posts: 1,055
Re: Unable to change the dimension of image in VB.NET

Normally to resizing an image is a bit more complicated

Code:
dim ImgRedimension as new bitmap(newWidth, newHeight)
dim g as graphics = graphics.fromimage(ImgRedimension)
g.Drawimage(image_of_base, divers_parameters)
ImgRedimension.save()
instead of [divers_parameters] you must use the overload with a source rectangle and destination rectangle (rectangles are 0.0 to img.width, img.height for source and dest with the new dimensions)
Reply With Quote
  #3  
Old 11-12-2009
Member
 
Join Date: Jun 2009
Posts: 384
Re: Unable to change the dimension of image in VB.NET

Hello, thank you for your answer, being that second year can you explain a little more simply please? Because I do not know what to do with graphics. Instead of pictbox.image.save I must put graphic.save? I do not see what you mean by source rectangle etc.
Reply With Quote
  #4  
Old 11-12-2009
Member
 
Join Date: Nov 2008
Posts: 1,055
Re: Unable to change the dimension of image in VB.NET

I've put almost all the code to resize an image, you must create a new dimension for the new, create graphics associated with this new image drawing the basic image we have on the graphics with the new size then the image created can be saved

DrawImage has 30 overloads, one must use an application that SrcRect and DestRect (just read the intellisense)

and
Code:
dim srcrect as new rectangle (0,0,picturebox.image.width,picturebox.image.height)
dim destcrect as new rectangle (0,0,newWidth,newHeight)
Reply With Quote
  #5  
Old 11-12-2009
Member
 
Join Date: Jun 2009
Posts: 384
Re: Unable to change the dimension of image in VB.NET

Code:
Dim ImgRedimension As New Bitmap(W, H)
Dim g As Graphics = Graphics.FromImage(ImgRedimension)
g.DrawImage(pictbox.Image, pictbox.DisplayRectangle.Location)
Try
    If type_file = ".png" Then
            ImgRedimension.Save(Server.MapPath("~/uploads/" & code_url & "/") + name_vig_file, Imaging.ImageFormat.Png)
    ElseIf type_file = ".jpg" Or type_file = ".jpeg" Then
            ImgRedimension.Save(Server.MapPath("~/uploads/" & code_url & "/") + name_vig_file, Imaging.ImageFormat.Jpeg)
    ElseIf type_file = ".gif" Then
            ImgRedimension.Save(Server.MapPath("~/uploads/" & code_url & "/") + name_vig_file, Imaging.ImageFormat.Gif)
    End If
Catch ex As Exception
    Console.WriteLine(ex.Message)
End Try
Here I replaced divers_parameter by pictbox.displayrectangle, but ultimately my image is not resized!
Reply With Quote
  #6  
Old 11-12-2009
Member
 
Join Date: Nov 2008
Posts: 1,055
Re: Unable to change the dimension of image in VB.NET

Code:
Dim imageFinal As System.Drawing.Image = Nothing
Dim g As System.Drawing.Graphics = Nothing
imageFinal = New System.Drawing.Bitmap(new_size.Width, new_size.Height)
g = System.Drawing.Graphics.FromImage(imageFinal)
Dim destRect As New System.Drawing.Rectangle(0, 0, new_size.Width, new_size.Height)
Dim srcRect As New System.Drawing.Rectangle(0, 0, IMG.Width, IMG.Height)
g.DrawImage(IMG, destRect, srcRect, System.Drawing.GraphicsUnit.Pixel)
imageFinal.save(...)
to adapt (IMG is the image of your PictureBox, new_size is the drawing.size of the new size desired)
Reply With Quote
Reply

  TechArena Community > Software > Software Development


Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Unable to change the dimension of image in VB.NET"
Thread Thread Starter Forum Replies Last Post
Unable to detect memory in Dell dimension Tana$ya Motherboard Processor & RAM 6 03-07-2011 11:29 AM
Unable to upgrade Dell Dimension B110 CPU Kim|ball Motherboard Processor & RAM 4 03-06-2011 11:34 AM
Unable to pass post in Dimension E310 Virr Hardware Peripherals 6 01-06-2011 11:11 PM
Unable to get any sound in Dell dimension 4700c The^Affleck Hardware Peripherals 5 21-04-2011 07:20 PM
Unable to get audio in Dimension 4700 Dechen Portable Devices 4 21-02-2011 07:43 PM


All times are GMT +5.5. The time now is 05:21 AM.