vb.net下载图片 vb保存图片( 二 )


属性vb.net下载图片,而且它是位图、图标、元文件或增强元文件vb.net下载图片,则图形将以原始文件同样的格式保存 。如果它是 GIF 或 JPEG 文件,则将保存为位图文件 。
2)AutoRedraw 属性,返回或设置从图形方法到持久图形的输出 。
该属性为True,可使 Form 对象或 PictureBox
控件的自动重绘有效 。图形和文本输出到屏幕 , 并存储在内存的图象中 。该对象不接受绘制事件,必要时,用存储在内存中的图象进行重绘 。
以下代码保存窗体装载并进行文字修饰的图片:
Option Explicit
Private Sub Command1_Click()
SavePicture Image, App.Path"\TEST"' 将图片保存到文件 。
End Sub
Private Sub Form_Click()
FontSize = 24
Print " 将图片保存到文件"
End Sub
Private Sub Form_Load()
Me.AutoRedraw = True
End Sub
3)若要保存为JPG图片需要采取使用API函数实现 。参考以下步骤和代码:
a.在图片框加载需要添加文字水印的图片 。
b.使用如下代码实现添加文字到图片框 。
Private Sub CmdEdit_Click() '修改
Dim strTxt As String
strTxt = "风雨无阻 拍摄"
Picture1.FontSize = 18
Picture1.CurrentY = Picture1.ScaleHeight - 30
Picture1.CurrentX = Picture1.ScaleWidth / 2 - Picture1.TextWidth(strTxt) / 2
Picture1.ForeColor = vbWhite
Picture1.FontItalic = True
Picture1.Print strTxt
End Sub
c.将以下API转换图片格式代码放置于标准模块 , 模块命名为saveApg 。
Option Explicit
Public Type GUID
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(0 To 7) As Byte
End Type
Public Type GdiplusStartupInput
GdiplusVersion As Long
DebugEventCallback As Long
SuppressBackgroundThread As Long
SuppressExternalCodecs As Long
End Type
Public Type EncoderParameter
GUID As GUID
NumberOfValues As Long
type As Long
Value As Long
End Type
Public Type EncoderParameters
Count As Long
Parameter As EncoderParameter
End Type
Public Declare Function GdiplusStartup Lib "GDIPlus" (token As Long, inputbuf As GdiplusStartupInput, ByVal outputbuf As Long) As Long
Public Declare Function GdiplusShutdown Lib "GDIPlus" (ByVal token As Long) As Long
Public Declare Function GdipCreateBitmapFromHBITMAP Lib "GDIPlus" (ByVal hbm As Long, ByVal hpal As Long, Bitmap As Long) As Long
Public Declare Function GdipDisposeImage Lib "GDIPlus" (ByVal Image As Long) As Long
Public Declare Function GdipSaveImageToFile Lib "GDIPlus" (ByVal Image As Long, ByVal fileName As Long, clsidEncoder As GUID, encoderParams As Any) As Long
Public Declare Function CLSIDFromString Lib "ole32" (ByVal str As Long, id As GUID) As Long
Public Declare Function GdipCreateBitmapFromFile Lib "GDIPlus" (ByVal fileName As Long, Bitmap As Long) As Long
Public Function PictureBoxSaveJPG(ByVal pict As StdPicture, ByVal fileName As String, Optional ByVal quality As Byte = 80) As Boolean
Dim tSI As GdiplusStartupInput
Dim lRes As Long
Dim lGDIP As Long
Dim lBitmap As Long
'初始化 GDI+
tSI.GdiplusVersion = 1
lRes = GdiplusStartup(lGDIP, tSI, 0)
If lRes = 0 Then
'从句柄创建 GDI+ 图像
lRes = GdipCreateBitmapFromHBITMAP(pict.Handle, 0, lBitmap)
If lRes = 0 Then
Dim tJpgEncoder As GUID
Dim tParams As EncoderParameters
'初始化解码器的GUID标识
CLSIDFromString StrPtr("{557CF401-1A04-11D3-9A73-0000F81EF32E}"), tJpgEncoder
'设置解码器参数
tParams.Count = 1
With tParams.Parameter ' Quality
'得到Quality参数的GUID标识
CLSIDFromString StrPtr("{1D5BE4B5-FA4A-452D-9CDD-5DB35105E7EB}"), .GUID
.NumberOfValues = 1

推荐阅读