i found out how to do this right
this is the url from which i got the code
http://www.daniweb.com/software-development/vbnet/threads/151127/correctly-displaying-image-in-picturebox[
^]
NOTE !! This will scale the image to "fit" the picturebox so if you decide to save the image it will save it using the scaled bitmap's resolution (if you load into your picturebox an image that has 1920x1080 you will save in a different resolution a lower one since it was scaled). To save the image with the same resolution do this :
lets say you want to load an image on form_load but before anything declare two variables like below
Quote:
Dim defaultHeight As Integer
Dim defaultWidth As Integer
Now in form_load event add this
Quote:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim PicBoxHeight As Integer
Dim PicBoxWidth As Integer
Dim ImageHeight As Integer
Dim ImageWidth As Integer
Dim TempImage As Image
Dim scale_factor As Single
PictureBox1.SizeMode = PictureBoxSizeMode.Normal
' Get control size
PicBoxHeight = PictureBox1.Height
PicBoxWidth = PictureBox1.Width
' Load the image. Change Image.FromFile() to match your code
TempImage = Image.FromFile(str)
defaultWidth = TempImage.Width 'is the width of your selected image (unscaled)
defaultHeight = TempImage.Height 'is the height of your selected image (unscaled)
' Get image size
ImageHeight = TempImage.Height
ImageWidth = TempImage.Width
' Calculate scale_factor
scale_factor = 1.0 ' No scaling by default i.e. image fits in the Box
' 1) Height
If ImageHeight > PicBoxHeight Then
' Reduce height first
scale_factor = CSng(PicBoxHeight / ImageHeight)
End If
' 2) Width. Notice, we do know now how much we have to scale down the height
' and the scale_factor affects width too
If (ImageWidth * scale_factor) > PicBoxWidth Then
' Scaled width exceeds Box's width, recalculate scale_factor
scale_factor = CSng(PicBoxWidth / ImageWidth)
End If
' Move image to control for resizing
PictureBox1.Image = TempImage
' Get the source bitmap.
Dim bm_source As New Bitmap(PictureBox1.Image)
' Make a bitmap for the result.
Dim bm_dest As New Bitmap( _
CInt(bm_source.Width * scale_factor), _
CInt(bm_source.Height * scale_factor))
' Make a Graphics object for the result Bitmap.
Dim gr_dest As Graphics = Graphics.FromImage(bm_dest)
' Copy the source image into the destination bitmap.
gr_dest.DrawImage(bm_source, 0, 0, _
bm_dest.Width + 1, _
bm_dest.Height + 1)
' Display the result.
PictureBox1.Image = bm_dest
Drawbitmap = New Bitmap(PictureBox1.Image)
PictureBox1.Height = Drawbitmap.Height
Drawgraphics = Graphics.FromImage(Drawbitmap)
End Sub
Picturebox1 is just an example replace it with your picturebox
Now when you click the save button in my case it's another picturebox (you could use a button) add this to the click event
Quote:
Private Sub PictureBox9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox9.Click
Dim bmp As New Bitmap(PictureBox1.Image, defaultWidth, defaultHeight) ' this will take the bitmap from your Picturebox1.image and scale it to it's original width and hight
bmp.Save("C:\image.jpg")
bmp.Dispose()
MessageBox.Show("Saved !")
End Sub
I hope there will be no misunderstandings when reading this . Enjoy and thank you for your replies once again.