简体   繁体   English

将放大的图像保存在图片框中?

[英]Save zoomed image in a picturebox?

I'm a newbie in vb.net (windows app) programming.我是 vb.net(Windows 应用程序)编程的新手。 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.但是我不知道的是如何将图像的当前大小保存为图像相对于面板大小的大小和当前position。

This is what my project looks like.这就是我的项目的样子。 See I can load an image and save the picture as is to the database.请参阅我可以加载图像并将图片按原样保存到数据库中。

What my project looks like我的项目是什么样的

My question is, how to save the current location and size relative to the panel size of the image if I zoom it?我的问题是,如果我缩放它,如何保存相对于图像面板大小的当前位置和大小?

How to save this image and resize the picturebox to the size of the panel and the current location of the zoomed image and the image itself如何保存此图像并将图片框调整为面板大小以及缩放图像的当前位置和图像本身

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.我使用了@dr.null 和@jtxkopt 的建议,它以某种方式起作用。 BUT the problem is, the output has also drawn the scrollbars on the panel.但问题是,output 还在面板上绘制了滚动条。

Output: This is my current output Output:这是我现在的 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.我已经在代码行之前使用了 panel.VerticalScroll.Visible = false 但它不起作用。

You can use the function DrawToBitmap to save modified image inside PictureBox.您可以使用DrawToBitmap将修改后的图像保存在 PictureBox 中。 Follow the below procedure.请遵循以下程序。

  1. Create a new bitmap with the same size and Pixel Format as the picturebox.创建一个新的 bitmap,其大小和像素格式与图片框相同。
  2. Call the DrawToBitmap function of the picturebox.调用picturebox的DrawToBitmap

For more details, examine the following example program.有关更多详细信息,请检查以下示例程序。

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

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.如果是这样,您可以通过多种方式和技术来实现, DrawToBitmap是最简单的方法。 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.使用PictureBox绘制方法和大小不会 output 所需的结果,因为当您应用缩放/比例因子时它的大小会发生变化。 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). output 图像大小是Panel客户端大小 (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 .其中pboxPictureBoxpnl是它的父Panel

SO73442247

That's it all if that is what you are after.如果这就是你所追求的,那就是一切。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM