0

I would like to know is there any way that I can save an image from picturebox based on picturebox.sizemode or save an image based on picturebox client size?

Currently, I noticed even though I set it as pictureboxfilter.sizemode = Normal. It will also save it as stretch mode.

Below are sample code:

Dim current2 As Bitmap = CType(PictureBoxFilter.Image, Bitmap)
Dim filepath As String = Environment.CurrentDirectory
Dim fileName2 As String = System.IO.Path.Combine(filepath, "name2.png")

Dim bmpt As New Bitmap(400, 400)

     Using g As Graphics = Graphics.FromImage(bmpt)

         g.DrawImage(current2, 0, 0,
         bmpt.Width + 1,
         bmpt.Height + 1)
         bmpt.Save(fileName2)
         bmpt.Dispose()
         g.Dispose()
     End Using
Safwan
  • 85
  • 2
  • 12
  • The `PictureBox` is irrelevant. You're calling `Save` on an `Image` object so that object is all that matters. You could call `DrawToBitmap` on the `PictureBox` control to get exactly what you see on screen drawn to a new `Bitmap` object that you created based on the `Size` of the control. That's not a great idea though, because the displayed image could be much smaller or much bigger than the original and so you'd be losing information either way. – jmcilhinney May 18 '20 at 06:44
  • You can take the `GetScaledImageRect()` method from here: [Disable Image blending on a PictureBox](https://stackoverflow.com/a/54726707/7444103). You can use the container Size (the PictureBox.ClientSize ) as the size of the destination rectangle. Pass to the method this size and the original Image (you can pass `PictureBoxFilter.Image`). It will return the Rectangle representing the Image scaled to the destination canvas size, without distorting it. – Jimi May 18 '20 at 06:53
  • Btw, no need to call `g.Dispose()` when you have already declared it with a `Using` statement. You could declare the Bitmap in the same way, since you're ditching it right after you have saved it. – Jimi May 18 '20 at 06:58
  • Also, don't modify the Bitmap size (those `bmpt.Width + 1` and `bmpt.Height + 1`, I mean). – Jimi May 18 '20 at 07:05

0 Answers0