【发布时间】:2016-03-10 21:52:29
【问题描述】:
在这个应用程序中,我正在处理用户从组合框中选择一个子文件名,并且在该子文件夹中最多有 5 张图片(在开始时)。这些图像的名称是相机输出它们的名称,这意味着我不知道文件名。这个应用程序的目的是让用户在小缩略图图片框中预览 1-5 个图像,然后一次选择一个并使用单选按钮选项中的文本字符串保存它们,按下“重命名”按钮后,代码然后保存所选图像作为选择的名称。
一切正常,但我有 2 个问题。
第一。保存图像后,文件夹中现在有超过 5 张 jpeg 图片,正在保存的这些图片将始终包含以下文字:
PicSpindle、PicRotorTop、PicRotorBottom、PicDunnageFinal 或 PicDunnageLayer
那么有没有办法使用我已经拥有的代码来避免在Directory.GetFiles 中包含这些字符串的 jpeg 文件?可能有更好的方法可以做到这一点,但这是我可以用我的知识汇总的全部内容。也许使用file.move 或以某种方式替换图像,而不是将图像保存为新名称并将更多 jpeg 文件添加到文件夹中?
第二次是一旦我知道如何在 jpeg getfile 名称中排除上述字符串,我需要使用我将添加的“清除图片”按钮释放和删除所有旧文件。
我在网上找到了一些代码,并且整天都在拼凑它。但是现在我遇到了障碍,找不到任何可以帮助我度过这一点的东西。
代码:
Imports System.Drawing.Imaging
Imports System.IO
Public Class Form1
Dim Pictype As String
Private HighlightedPictureBox As PictureBox = Nothing
Private Sub cmbPartNumber_TextChanged(sender As Object, e As EventArgs) Handles cmbPartNumber.TextChanged
Dim FileName As String = cmbPartNumber.Text
lblRenameAs.Text = Pictype & FileName
Label1.Visible = True
picPreview1.Visible = True
picPreview2.Visible = True
picPreview3.Visible = True
picPreview4.Visible = True
picPreview5.Visible = True
Picture_Preview()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim Folder = From dir In IO.Directory.GetDirectories("\\HOMESHARE01\Public\Reference Cards\Completed Reference Cards by Part Number")
Select IO.Path.GetFileName(dir)
cmbPartNumber.Items.AddRange(Folder.ToArray)
End Sub
Private Sub btnSavePic_Click(sender As Object, e As EventArgs) Handles btnSavePic.Click
Dim FileName As String = cmbPartNumber.Text
'Check if no radio button checked
If Not (radSpindle.Checked = True Or radRotorTop.Checked = True Or radRotorBottom.Checked = True Or radDunnageFinal.Checked = True Or radDunnageLayer.Checked = True) Then
MsgBox("Please Select Picture Type Before Renaming")
Exit Sub
End If
'Check which radio button is checked
If radSpindle.Checked = True Then
Pictype = "PicSpindle"
End If
If radRotorTop.Checked = True Then
Pictype = "PicRotorTop"
End If
If radRotorBottom.Checked = True Then
Pictype = "PicRotorBottom"
End If
If radDunnageFinal.Checked = True Then
Pictype = "PicDunnageFinal"
End If
If radDunnageLayer.Checked = True Then
Pictype = "PicDunnageLayer"
End If
If Not picSelectedPic.Image Is Nothing Then
picSelectedPic.Image.Save("\\HOMESHARE01\Public\Reference Cards\Completed Reference Cards by Part Number" & "\" & FileName & "\" & lblRenameAs.Text & ".jpg", ImageFormat.Jpeg)
MsgBox("Picture: " & lblRenameAs.Text & " Was Saved In Folder: " & FileName)
Else
MsgBox("Please Select Picture Before Renaming")
Exit Sub
End If
End Sub
Public Sub Picture_Preview()
Dim FileName As String = cmbPartNumber.Text
Dim pics() As PictureBox
Dim List() As String = Directory.GetFiles("\\HOMESHARE01\Public\Reference Cards\Completed Reference Cards by Part Number" & "\" & FileName, "*.jpg")
If List.Length = "5" Then
pics = {picPreview1, picPreview2, picPreview3, picPreview4, picPreview5}
End If
If List.Length = "4" Then
pics = {picPreview1, picPreview2, picPreview3, picPreview4}
End If
If List.Length = "3" Then
pics = {picPreview1, picPreview2, picPreview3}
End If
If List.Length = "2" Then
pics = {picPreview1, picPreview2}
End If
If List.Length = "1" Then
pics = {picPreview1}
End If
If List.Length = "0" Then
MsgBox("No Pictures in File")
Exit Sub
End If
For i As Integer = 0 To pics.Count - 1
pics(i).Image = Image.FromFile(List(i))
Next
picPreview1.SizeMode = PictureBoxSizeMode.StretchImage
picPreview2.SizeMode = PictureBoxSizeMode.StretchImage
picPreview3.SizeMode = PictureBoxSizeMode.StretchImage
picPreview4.SizeMode = PictureBoxSizeMode.StretchImage
picPreview5.SizeMode = PictureBoxSizeMode.StretchImage
picSelectedPic.SizeMode = PictureBoxSizeMode.StretchImage
End Sub
Private Sub picPreview1_Click(sender As Object, e As EventArgs) Handles picPreview1.Click
picSelectedPic.Image = Nothing
'Get the rectangle of the control and inflate it to represent the border area
Dim BorderBounds As Rectangle = DirectCast(sender, PictureBox).ClientRectangle
BorderBounds.Inflate(-1, -1)
'Use ControlPaint to draw the border.
'Change the Color.Red parameter to your own colour below.
ControlPaint.DrawBorder(DirectCast(sender, PictureBox).CreateGraphics,
BorderBounds,
Color.Red,
ButtonBorderStyle.Solid)
If Not (HighlightedPictureBox Is Nothing) Then
'Remove the border of the last PictureBox
HighlightedPictureBox.Invalidate()
End If
'Rememeber the last highlighted PictureBox
HighlightedPictureBox = CType(sender, PictureBox)
picSelectedPic.Image = picPreview1.Image
End Sub
Private Sub picPreview2_Click(sender As Object, e As EventArgs) Handles picPreview2.Click
picSelectedPic.Image = Nothing
'Get the rectangle of the control and inflate it to represent the border area
Dim BorderBounds As Rectangle = DirectCast(sender, PictureBox).ClientRectangle
BorderBounds.Inflate(-1, -1)
'Use ControlPaint to draw the border.
'Change the Color.Red parameter to your own colour below.
ControlPaint.DrawBorder(DirectCast(sender, PictureBox).CreateGraphics,
BorderBounds,
Color.Red,
ButtonBorderStyle.Solid)
If Not (HighlightedPictureBox Is Nothing) Then
'Remove the border of the last PictureBox
HighlightedPictureBox.Invalidate()
End If
'Rememeber the last highlighted PictureBox
HighlightedPictureBox = CType(sender, PictureBox)
picSelectedPic.Image = picPreview2.Image
End Sub
Private Sub picPreview3_Click(sender As Object, e As EventArgs) Handles picPreview3.Click
picSelectedPic.Image = Nothing
'Get the rectangle of the control and inflate it to represent the border area
Dim BorderBounds As Rectangle = DirectCast(sender, PictureBox).ClientRectangle
BorderBounds.Inflate(-1, -1)
'Use ControlPaint to draw the border.
'Change the Color.Red parameter to your own colour below.
ControlPaint.DrawBorder(DirectCast(sender, PictureBox).CreateGraphics,
BorderBounds,
Color.Red,
ButtonBorderStyle.Solid)
If Not (HighlightedPictureBox Is Nothing) Then
'Remove the border of the last PictureBox
HighlightedPictureBox.Invalidate()
End If
'Rememeber the last highlighted PictureBox
HighlightedPictureBox = CType(sender, PictureBox)
picSelectedPic.Image = picPreview3.Image
End Sub
Private Sub picPreview4_Click(sender As Object, e As EventArgs) Handles picPreview4.Click
picSelectedPic.Image = Nothing
'Get the rectangle of the control and inflate it to represent the border area
Dim BorderBounds As Rectangle = DirectCast(sender, PictureBox).ClientRectangle
BorderBounds.Inflate(-1, -1)
'Use ControlPaint to draw the border.
'Change the Color.Red parameter to your own colour below.
ControlPaint.DrawBorder(DirectCast(sender, PictureBox).CreateGraphics,
BorderBounds,
Color.Red,
ButtonBorderStyle.Solid)
If Not (HighlightedPictureBox Is Nothing) Then
'Remove the border of the last PictureBox
HighlightedPictureBox.Invalidate()
End If
'Rememeber the last highlighted PictureBox
HighlightedPictureBox = CType(sender, PictureBox)
picSelectedPic.Image = picPreview4.Image
End Sub
Private Sub picPreview5_Click(sender As Object, e As EventArgs) Handles picPreview5.Click
picSelectedPic.Image = Nothing
'Get the rectangle of the control and inflate it to represent the border area
Dim BorderBounds As Rectangle = DirectCast(sender, PictureBox).ClientRectangle
BorderBounds.Inflate(-1, -1)
'Use ControlPaint to draw the border.
'Change the Color.Red parameter to your own colour below.
ControlPaint.DrawBorder(DirectCast(sender, PictureBox).CreateGraphics,
BorderBounds,
Color.Red,
ButtonBorderStyle.Solid)
If Not (HighlightedPictureBox Is Nothing) Then
'Remove the border of the last PictureBox
HighlightedPictureBox.Invalidate()
End If
'Rememeber the last highlighted PictureBox
HighlightedPictureBox = CType(sender, PictureBox)
picSelectedPic.Image = picPreview5.Image
End Sub
Private Sub radRotorTop_Click(sender As Object, e As EventArgs) Handles radRotorTop.Click
Dim FileName As String = cmbPartNumber.Text
Pictype = "PicRotorTop"
lblRenameAs.Text = Pictype & FileName
End Sub
Private Sub radRotorBottom_Click(sender As Object, e As EventArgs) Handles radRotorBottom.Click
Dim FileName As String = cmbPartNumber.Text
Pictype = "PicRotorBottom"
lblRenameAs.Text = Pictype & FileName
End Sub
Private Sub radDunnageLayer_Click(sender As Object, e As EventArgs) Handles radDunnageLayer.Click
Dim FileName As String = cmbPartNumber.Text
Pictype = "PicDunnageLayer"
lblRenameAs.Text = Pictype & FileName
End Sub
Private Sub radDunnageFinal_Click(sender As Object, e As EventArgs) Handles radDunnageFinal.Click
Dim FileName As String = cmbPartNumber.Text
Pictype = "PicDunnageFinal"
lblRenameAs.Text = Pictype & FileName
End Sub
Private Sub radSpindle_Click(sender As Object, e As EventArgs) Handles radSpindle.Click
Dim FileName As String = cmbPartNumber.Text
Pictype = "PicSpindle"
lblRenameAs.Text = Pictype & FileName
End Sub
Private Sub btnPurgeFolder_Click(sender As Object, e As EventArgs) Handles btnPurgeFolder.Click
End Sub
End Class
获取文件代码:
Public Sub Picture_Preview()
Dim FileName As String = cmbPartNumber.Text
Dim pics() As PictureBox
Dim List() As String = Directory.GetFiles("\\HOMESHARE01\Public\Reference Cards\Completed Reference Cards by Part Number" & "\" & FileName, "*.jpg")
If List.Length = "5" Then
pics = {picPreview1, picPreview2, picPreview3, picPreview4, picPreview5}
End If
If List.Length = "4" Then
pics = {picPreview1, picPreview2, picPreview3, picPreview4}
End If
If List.Length = "3" Then
pics = {picPreview1, picPreview2, picPreview3}
End If
If List.Length = "2" Then
pics = {picPreview1, picPreview2}
End If
If List.Length = "1" Then
pics = {picPreview1}
End If
If List.Length = "0" Then
MsgBox("No Pictures in File")
Exit Sub
End If
For i As Integer = 0 To pics.Count - 1
pics(i).Image = Image.FromFile(List(i))
Next
picPreview1.SizeMode = PictureBoxSizeMode.StretchImage
picPreview2.SizeMode = PictureBoxSizeMode.StretchImage
picPreview3.SizeMode = PictureBoxSizeMode.StretchImage
picPreview4.SizeMode = PictureBoxSizeMode.StretchImage
picPreview5.SizeMode = PictureBoxSizeMode.StretchImage
picSelectedPic.SizeMode = PictureBoxSizeMode.StretchImage
End Sub
【问题讨论】:
-
有很多代码需要查看。 “这些正在保存的图片将始终包含这些文字”,您是指这些图片的文件名吗?
-
顺便说一下,
Image.Save作为 jpeg 可能会产生令人失望的结果。您可能想查看Image.Save Method (String, ImageCodecInfo, EncoderParameters) 并使用更高质量的设置(例如 80&)。