原创文章,转载请注明出处!
Code:
Imports System.IO Public Class Form1 Dim picBoxX, picBoxY, picX, picY, FormWith, FormHeight As Int32 Dim num1, num2, num3, num4 As Int32 Dim localFilePath, FilePathEx(), FilePath, newFileName As String Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load picBoxX = PictureBox1.Width picBoxY = PictureBox1.Height FormWith = Me.Width FormHeight = Me.Height NumericUpDown3.Value = 0 NumericUpDown4.Value = 0 NumericUpDown3.Maximum = 200 '最大两倍放大 NumericUpDown4.Maximum = 200 '最大两倍放大 End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim mystream As Stream 'OpenFileDialog1.InitialDirectory = Application.StartupPath 'or System.IO.Directory.GetCurrentDirectory() OpenFileDialog1.InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop) OpenFileDialog1.Filter = " 支持格式|*.bmp;*.gif;*.jpg;*.png|BMP 文件(*.BMP)|*.BMP|JPG 文件(*.JPG)|*.JPG|GIF 文件(*.GIF)|*.GIF|PNG 文件(*.PNG)|*.PNG" OpenFileDialog1.FilterIndex = 3 OpenFileDialog1.RestoreDirectory = True If OpenFileDialog1.ShowDialog() = DialogResult.OK Then mystream = OpenFileDialog1.OpenFile() If Not (mystream Is Nothing) Then PictureBox1.Image = System.Drawing.Image.FromFile(OpenFileDialog1.FileName) Me.PictureBox1.ImageLocation = OpenFileDialog1.FileName Me.TextBox1.Text = Me.PictureBox1.ImageLocation PictureBox1.SizeMode = PictureBoxSizeMode.Zoom '缩放模式 picX = PictureBox1.Image.Width picY = PictureBox1.Image.Height picBoxX = Me.PictureBox1.Width picBoxY = Me.PictureBox1.Height Label1.Text = "宽度已缩放为:" & CInt((picBoxX / picX) * 100) & "%; 高度已缩放为:" & CInt((picBoxY / picY) * 100) & "%" NumericUpDown1.Maximum = picX + picX '最大两倍放大 NumericUpDown2.Maximum = picY + picY '最大两倍放大 NumericUpDown1.Value = picX.ToString NumericUpDown2.Value = picY.ToString num1 = picX num2 = picY num3 = 100 num4 = 100 NumericUpDown1.Value = num1.ToString NumericUpDown2.Value = num2.ToString NumericUpDown3.Value = num3 NumericUpDown4.Value = num4 mystream.Close() End If End If End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click If TextBox1.Text <> "" Then PictureBox1.Image = System.Drawing.Image.FromFile(TextBox1.Text) If Button2.Text = "图片真实大小显示" Then PictureBox1.AutoSize = True '缩放模式 Me.Width = picX + 60 Me.Height = picY + 60 Button2.Text = "图片缩放预览显示" Label1.Text = "宽度已缩放为:100%" & "; 高度已缩放为:100%" ElseIf Button2.Text = "图片缩放预览显示" Then PictureBox1.AutoSize = False Me.Width = FormWith Me.Height = FormHeight PictureBox1.SizeMode = PictureBoxSizeMode.Zoom '缩放模式 Button2.Text = "图片真实大小显示" Label1.Text = "宽度已缩放为:" & CInt((picBoxX / picX) * 100) & "%; 高度已缩放为:" & CInt((picBoxY / picY) * 100) & "%" End If Else MsgBox("请载入图片...") End If End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click SaveFileDialog1.Filter = " 支持格式|*.bmp;*.gif;*.jpg;*.png|BMP 文件(*.BMP)|*.BMP|JPG 文件(*.JPG)|*.JPG|GIF 文件(*.GIF)|*.GIF|PNG 文件(*.PNG)|*.PNG" SaveFileDialog1.FilterIndex = 3 SaveFileDialog1.RestoreDirectory = True localFilePath = Me.TextBox1.Text FilePathEx = localFilePath.Split("\\") FilePath = "" For i = 0 To FilePathEx.Length - 2 FilePath &= FilePathEx(i) & "\" Next newFileName = Now.ToString("yyyymmmddHHmmss") & " Width_" & num1 & "_Height_" & num2 & " " & FilePathEx(FilePathEx.Length - 1) Try Dim saveImage As New System.Drawing.Bitmap(Me.PictureBox1.Image, num1, num2) saveImage.Save(FilePath & newFileName) PictureBox1.Image = System.Drawing.Image.FromFile(FilePath & newFileName) Label1.Text = "新图片" & newFileName & "已保存到桌面" If num1 < picBoxX And num2 < picBoxY Then PictureBox1.SizeMode = PictureBoxSizeMode.Normal PictureBox1.SizeMode = PictureBoxSizeMode.CenterImage Else PictureBox1.AutoSize = True Me.Width = num1 + 100 Me.Height = num2 + 180 End If Catch ex As Exception MsgBox(ex.Message) End Try End Sub Private Sub NumericUpDown1_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles NumericUpDown1.ValueChanged num1 = CInt(NumericUpDown1.Value) End Sub Private Sub NumericUpDown2_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDown2.ValueChanged num2 = CInt(NumericUpDown2.Value) End Sub Private Sub NumericUpDown3_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles NumericUpDown3.ValueChanged NumericUpDown1.Value = (CInt(NumericUpDown3.Value) / 100) * picX num1 = NumericUpDown1.Value End Sub Private Sub NumericUpDown4_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles NumericUpDown4.ValueChanged NumericUpDown2.Value = (CInt(NumericUpDown4.Value) / 100) * picY num2 = NumericUpDown2.Value End Sub
End Class
程序界面1:
程序界面2: