在Form1.vb代码中,我放置了一个功能来在PictureBox1中显示图像。 然后我创建了一个具有功能的模块来保存图像。 所以在Windows窗体中我添加了一个调用模块的按钮。
模块是:
Imports System.Windows.Forms
Module Salvataggio
Dim SaveFileDialog1 As New SaveFileDialog
Dim PictureBox1 As New PictureBox
Public idname As String
Public Sub Save()
If PictureBox1.Image Is Nothing Then
MessageBox.Show("You haven't created an image yet.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
SaveFileDialog1.FileName = idname
SaveFileDialog1.Filter = "(*.png)|*.png|(*.jpg)|*.jpg|(*.bmp)|*.bmp"
SaveFileDialog1.ShowDialog()
PictureBox1.Image.Save(SaveFileDialog1.FileName)
End If
End Sub
End Module
Form1以这种方式设置图像:
Private Sub ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ToolStripMenuItem.Click
PictureBox1.Image = My.Resources.IT_CI_F
end sub
Form1调用模块:
Public Sub ToolStripButton1_Click(sender As Object, e As EventArgs) Handles ToolStripSave.Click
Save()
End Sub
基本上我在图片框中设置图像然后我尝试保存后,模块似乎并不知道我设置了图像。他认为图片框是空的。
答案 0 :(得分:1)
此代码不属于模块。您应该做的是创建一个从具有SaveAs
功能的图片框派生的自定义控件。像这样:
Public Class UIPictureBox
Inherits PictureBox
Public Sub SaveAs(name As String)
If (Me.Image Is Nothing) Then
MessageBox.Show("You haven't created an image yet.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
Using dialog As New SaveFileDialog()
With dialog
.FileName = name
.Filter = "(*.png)|*.png|(*.jpg)|*.jpg|(*.bmp)|*.bmp"
If (.ShowDialog() = Windows.Forms.DialogResult.OK) Then
Select Case IO.Path.GetExtension(.FileName).ToLower()
Case ".png"
Me.Image.Save(.FileName, Imaging.ImageFormat.Png)
Case ".jpg"
Me.Image.Save(.FileName, Imaging.ImageFormat.Jpeg)
Case ".bmp"
Me.Image.Save(.FileName, Imaging.ImageFormat.Bmp)
Case Else
Throw New Exception("File extension not supported")
End Select
End If
End With
End Using
End If
End Sub
End Class
答案 1 :(得分:0)
这是因为你在模块中创建了一个new
pictureBox,这与你在Form中的对象不同。您可以向Save
方法添加参数:
Dim SaveFileDialog1 As New SaveFileDialog
Public idname As String
Public Sub Save(PictureBox1 As PictureBox)
If PictureBox1.Image Is Nothing Then
MessageBox.Show("You haven't created an image yet.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
SaveFileDialog1.FileName = idname
SaveFileDialog1.Filter = "(*.png)|*.png|(*.jpg)|*.jpg|(*.bmp)|*.bmp"
SaveFileDialog1.ShowDialog()
PictureBox1.Image.Save(SaveFileDialog1.FileName)
End If
End Sub
然后传递PictureBox
作为参数:
Public Sub ToolStripButton1_Click(sender As Object, e As EventArgs) Handles ToolStripSave.Click
Save(PictureBox1)
End Sub