crop image & save it to new file, howto?

I have 3200x2200 resolution image, now I want to crop Image from pixel 1600,1100 until 3200,2200(1600x1100 res). and save it to the new file

do you have the sample code

I'll appreciate that

 

I just knew how to save :

If SaveFileDialog1.ShowDialog(Me) = Windows.Forms.DialogResult.OK Then

  t.Save(SaveFileDialog1.FileName, Imaging.ImageFormat.Jpeg)

End If

but the problem is how to save the file with another image format(JPG, GIF) according to the savefiledialog

 

thanks




Answer this question

crop image & save it to new file, howto?

  • MDesigner

    I haven't tried this myself, but Google gave me: http://abstractvb.com/code.asp A=1088

    I also found this example: http://www.planet-source-code.com/vb/scripts/ShowCode.asp txtCodeId=1755&lngWId=10&txtForceRefresh=82820062574120111

     

    Hope that helps a bit - Robert Hoath


  • shmulik_segal

    I was looking at this question in your other thread... I'll post a reply there. (but I don't have any magic answers, sorry)



  • markse

    Roberts Hoath's answer has been tried and tested many times in the past and it's the easiest way to do it. No need to get into encoders etc.

    Though you may need saveDialog.FileName.ToUpper.EndsWith

    Rgds,


  • Jaime82

    Here's a code snipit that accomplishes your goal.

    This should be much faster than a pixel by pixel transfer as done in the code loop previously posted. This code also utilizes the Filter properties of the FileDialog - since the dialog must be set on one of the extension filter choices, you can use this value to determine the type of file to save. This is better than parsing the extension - suppose the user supplies some extension you didn't code for, they would have a file name that doesn't match it's file type.

    Set the Filter property of the FileDialog to: JPEG (*.jpg)|*.jpg|GIFF (*.gif)|*.gif|Bitmap (*.bmp)|*.bmp

    Then use this code:

    If Me.SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then

    Dim ifmt As System.Drawing.Imaging.ImageFormat = System.Drawing.Imaging.ImageFormat.Jpeg

    Select Case Me.SaveFileDialog1.FilterIndex

    Case 1

    ifmt = System.Drawing.Imaging.ImageFormat.Jpeg

    Case 2

    ifmt = System.Drawing.Imaging.ImageFormat.Gif

    Case 3

    ifmt = System.Drawing.Imaging.ImageFormat.Bmp

    End Select

    Dim obmp As New Bitmap("c:\MyOriginalImage.jpg")

    Dim nbmp As New Bitmap(1600, 1100)

    Dim gfx As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(nbmp)

    gfx.DrawImage(obmp, New Rectangle(New Point(0, 0), nbmp.Size), 1600, 1100, 1600, 1100, GraphicsUnit.Pixel)

    gfx.Dispose()

    nbmp.Save(Me.SaveFileDialog1.FileName, ifmt)

    End If

    Hope that helps!



  • technica

    Does this help you :

    Dim saveDialog As New SaveFileDialog

    If saveDialog.ShowDialog = Windows.Forms.DialogResult.OK Then

    If saveDialog.FileName.EndsWith("JPEG") Then

    PictureBox1.Image.Save(saveDialog.FileName, Imaging.ImageFormat.Jpeg)

    ElseIf saveDialog.FileName.EndsWith("BMP") Then

    PictureBox1.Image.Save(saveDialog.FileName, Imaging.ImageFormat.Bmp)

    End If

    End If

     

    ect ect..


  • KluchCode

    rkimble..

    do you have tricks so that I can only save the file into shared network folder only so we can't browse the local folder



  • Codata

    THANKS A LOT for the code :

    Dim obmp As New Bitmap("c:\MyOriginalImage.jpg")

    Dim nbmp As New Bitmap(1600, 1100)

    Dim gfx As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(nbmp)

    gfx.DrawImage(obmp, New Rectangle(New Point(0, 0), nbmp.Size), 1600, 1100, 1600, 1100, GraphicsUnit.Pixel)

    gfx.Dispose()

    nbmp.Save(Me.SaveFileDialog1.FileName, ifmt)

    YOU ARE GENIUS!!!!

    thanks..keep up the good work, keep helping me :lol:



  • raq

    thanks for all the reply.



  • GTH

    Is there any simple way to do that at the permanent pixel

  • Attila Fogel

    this is how I crop :

    dim m as new picturebox

    Dim tmpbitmap As Bitmap

    Dim t As New Bitmap(1600, 1100)

    Dim i, j As Integer

    i = 0

    j = 0

    For x = 1600 To 3200

    For y = 1100 To 2200

    Dim pixelColor As Color = tmpbitmap.GetPixel(x, y)

    t.SetPixel(i, j, pixelColor)

    j = j + 1

    Next y

    j = 0

    i = i + 1

    Next x

    m.Width = 1600

    M.Height = 1100

    M.SizeMode = PictureBoxSizeMode.StretchImage

    M.Image = t

    But how to save new image if its according to savefiledialog extension

    please..



  • crop image & save it to new file, howto?