图片缩小:缩小至60 80以内
Dim file As String = "C:\测试.jpg"
Dim img As image = getImage(file)
Dim bmp As bitmap
If img.width > 80 Then
If 80 * (img.height / img.width) > 60 Then
bmp = new bitmap(img, 80*(60/(80*(img.height/img.width))), 60)
Else
bmp = new bitmap(img, 80, 80 * (img.height / img.width))
End If
End If
bmp.save("C:\缩略图.jpg")
bmp.Dispose
质量保存
Dim img As image = getimage("g:\aaa.jpg")
Dim bmp As new bitmap(img.width, img.height)
Dim g = graphics.fromimage(bmp)
g.DrawImage(img, 0, 0, img.Width, img.Height)
Dim jpgEncoder As ImageCodecInfo
Dim codecs() As ImageCodecInfo = ImageCodecInfo.GetImageDecoders
For Each codec As ImageCodecInfo In codecs
If (codec.FormatID = ImageFormat.Jpeg.Guid) Then
jpgEncoder = codec
Exit For
End If
Next
Dim myEncoder As System.Drawing.Imaging.Encoder = System.Drawing.Imaging.Encoder.Quality
Dim myEncoderParameters As EncoderParameters = New EncoderParameters(1)
Dim myEncoderParameter As EncoderParameter = New EncoderParameter(myEncoder, 30) ' 质量级别 0 对应于最大压缩,而质量级别 100 对应于最小压缩
myEncoderParameters.Param(0) = myEncoderParameter
bmp.Save("g:\压缩图aaa.jpg", jpgEncoder, myEncoderParameters)
bmp.dispose
g.dispose
图片黑白
Dim dlg As New OpenFileDialog '定义一个新的OpenFileDialog
dlg.Filter = "图片文件|*.jpg;*.png" '设置筛选器
If dlg.ShowDialog = DialogResult.OK Then '如果用户单击了确定按钮
Dim omg As Image = GetImage(dlg.FileName)
Windows.Forms.ControlPaint.DrawImageDisabled(Graphics.FromImage(omg), omg, 0, 0, Color.Transparent)
Forms("窗口1").Controls("PictureBox1").Image = omg
End If