Reputation: 1
I'm a newbie in vb.net (windows app) programming. How can I save a zoomed image in a picturebox.
See, I have a panel and put a picturebox (set to zoom) inside and set the panel to autoscroll to accomodate the size of the image. I can zoom in and out of the image. I can save the picture as is using a memorystream and save it to the database (access). But the thing I don't know is how to save the current size of the image to the size and current position of the image relative to the size of the panel.
This is what my project looks like. See I can load an image and save the picture as is to the database.
My question is, how to save the current location and size relative to the panel size of the image if I zoom it?
I hope you understand my question (sorry if my english is bad, it is not my native language).
------edit------
UPDATE: I was able to save the zoomed image from the picturebox inside the panel. I used @dr.null and @jtxkopt suggestion and it works somehow. BUT the problem is, the output has also drawn the scrollbars on the panel.
Output: This is my current output
This is my code:
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
SaveFileDialog1.Filter = "Image Files|*.jpg; *.png; *.bmp"
Panel3.HorizontalScroll.Visible = False
Panel3.VerticalScroll.Visible = False
If SaveFileDialog1.ShowDialog = DialogResult.OK Then
Dim imageRectangle = New Rectangle(Point.Empty, picUser.Image.Size)
Dim safeCropRectangle = Rectangle.Intersect(imageRectangle, Panel3.DisplayRectangle)
Dim bmp As Bitmap = New Bitmap(Panel3.Width, Panel3.Height, picUser.Image.PixelFormat)
Panel3.DrawToBitmap(bmp, safeCropRectangle)
bmp.Save(SaveFileDialog1.FileName)
End If
End Sub
What can I do to remove the scroll bars? I already used the panel.VerticalScroll.Visible = false before the lines of code but it doesn't work.
Upvotes: 0
Views: 512
Reputation: 4695
I understand that you need to crop the displayed region of a zoomed in image. If so, you can achieve that through many ways and techniques, DrawToBitmap
is the easiest approach. Please note:
Using the PictureBox
draw method and size doesn't output the required result because it's size changes when you apply the zoom/scale factor. In other words, the result is either a smaller or bigger image of the original one according to the zoom factor.
The output image size is the Panel
client size (WYSIWYG).
Hence, you just need to do:
Dim imgRect = pnl.ClientRectangle
Dim bmp = New Bitmap(imgRect.Width, imgRect.Height, pbox.Image.PixelFormat)
pnl.DrawToBitmap(bmp, imgRect)
'Save it...
where pbox is the PictureBox
and pnl is it's parent Panel
.
That's it all if that is what you are after.
Upvotes: 0
Reputation: 1159
You can use the function DrawToBitmap
to save modified image inside PictureBox.
Follow the below procedure.
DrawToBitmap
function of the picturebox.For more details, examine the following example program. Using DrawToBitmap
, you can set the PictureBox.SizeMode
property to whatever you want and save the modified image to a file.
PS: This example code is for giving the idea of how you can save the image of PictureBox
in any SizeMode
, not for providing a solution to the question with/without its complete details.
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Threading.Tasks
Imports System.Windows.Forms
Namespace PictureboxZoomSave
Public Class MainForm
Inherits Form
Public Sub New()
InitializeComponent()
End Sub
Private openFileDialog As OpenFileDialog = New OpenFileDialog()
Private saveFileDialog As SaveFileDialog = New SaveFileDialog()
Private image As Image
Private Sub OpenImageButton_click(ByVal sender As Object, ByVal e As EventArgs)
openFileDialog.Filter = "Image Files|*.jpg; *.png"
If openFileDialog.ShowDialog() = DialogResult.OK Then
If image IsNot Nothing Then image.Dispose()
image = Image.FromFile(openFileDialog.FileName)
pictureBox1.Image = image
End If
End Sub
Private Sub SaveImageButton_click(ByVal sender As Object, ByVal e As EventArgs)
saveFileDialog.Filter = "Image Files|*.jpg; *.png"
If saveFileDialog.ShowDialog() = DialogResult.OK Then
Dim bitmap As Bitmap = New Bitmap(pictureBox1.Width, pictureBox1.Height, image.PixelFormat)
pictureBox1.DrawToBitmap(bitmap, New Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height))
bitmap.Save(saveFileDialog.FileName)
End If
End Sub
<STAThread>
Private Shared Sub Main()
Application.EnableVisualStyles()
Application.SetCompatibleTextRenderingDefault(False)
Application.Run(New MainForm())
End Sub
Private components As System.ComponentModel.IContainer = Nothing
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing AndAlso (components IsNot Nothing) Then
components.Dispose()
End If
MyBase.Dispose(disposing)
End Sub
Private Sub InitializeComponent()
Me.pictureBox1 = New System.Windows.Forms.PictureBox()
Me.OpenImageButton = New System.Windows.Forms.Button()
Me.SaveImageButton = New System.Windows.Forms.Button()
(CType((Me.pictureBox1), System.ComponentModel.ISupportInitialize)).BeginInit()
Me.SuspendLayout()
Me.pictureBox1.Location = New System.Drawing.Point(12, 12)
Me.pictureBox1.Name = "pictureBox1"
Me.pictureBox1.Size = New System.Drawing.Size(200, 191)
Me.pictureBox1.TabIndex = 0
Me.pictureBox1.TabStop = False
Me.OpenImageButton.Location = New System.Drawing.Point(229, 12)
Me.OpenImageButton.Name = "button1"
Me.OpenImageButton.Size = New System.Drawing.Size(75, 23)
Me.OpenImageButton.TabIndex = 1
Me.OpenImageButton.Text = "Open"
Me.OpenImageButton.UseVisualStyleBackColor = True
AddHandler Me.OpenImageButton.Click, New System.EventHandler(AddressOf Me.OpenImageButton_click)
Me.SaveImageButton.Location = New System.Drawing.Point(229, 42)
Me.SaveImageButton.Name = "button2"
Me.SaveImageButton.Size = New System.Drawing.Size(75, 23)
Me.SaveImageButton.TabIndex = 2
Me.SaveImageButton.Text = "Save"
Me.SaveImageButton.UseVisualStyleBackColor = True
AddHandler Me.SaveImageButton.Click, New System.EventHandler(AddressOf Me.SaveImageButton_click)
Me.AutoScaleDimensions = New System.Drawing.SizeF(6F, 13F)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(800, 450)
Me.Controls.Add(Me.SaveImageButton)
Me.Controls.Add(Me.OpenImageButton)
Me.Controls.Add(Me.pictureBox1)
Me.Name = "PictureBoxZoomSave"
Me.Text = "PictureBoxZoomSave"
(CType((Me.pictureBox1), System.ComponentModel.ISupportInitialize)).EndInit()
Me.ResumeLayout(False)
End Sub
Private pictureBox1 As System.Windows.Forms.PictureBox
Private OpenImageButton As System.Windows.Forms.Button
Private SaveImageButton As System.Windows.Forms.Button
End Class
End Namespace
Upvotes: 0