如何在VB中保存图片
发布网友
发布时间:2022-04-29 11:27
我来回答
共4个回答
热心网友
时间:2022-06-26 22:28
VB6.0可使用SavePicture 语句来保存窗体或图像框中经修改的图片。
1)SavePicture 语句,从对象或控件(如果有一个与其相关)的 Picture 或 Image 属性中将图形保存到文件中。
说明:无论在设计时还是运行时图形从文件加载到对象的 Picture
属性,而且它是位图、图标、元文件或增强元文件,则图形将以原始文件同样的格式保存。如果它是 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
.type = 4
.Value = VarPtr(quality)
End With
'保存图像
lRes = GdipSaveImageToFile(lBitmap, StrPtr(fileName), tJpgEncoder, tParams)
'销毁GDI+图像
GdipDisposeImage lBitmap
End If
'销毁 GDI+
GdiplusShutdown lGDIP
End If
If lRes Then
PictureBoxSaveJPG = False
Else
PictureBoxSaveJPG = True
End If
End Function
d.使用以下代码实现将加好文字水印图片保存为jpg格式图片。
Private Sub Command3_Click() '保存为.jpg图片
' 设置“CancelError”为 True
CommonDialog1.CancelError = True
On Error GoTo ErrHandler
' 设置标志
CommonDialog1.Flags = cdlOFNHideReadOnly
' 设置过滤器
CommonDialog1.Filter = "JPEG Files" & "(*.jpg)|*.jpg"
' 指定缺省的过滤器
CommonDialog1.FilterIndex = 2
' 显示“打开”对话框
CommonDialog1.ShowSave
' 显示选定文件的名字
'MsgBox CommonDialog1.fileName
Set Picture2.Picture = Picture1.Image '转移Picture1所绘图为Picture2.Picture赋值
Dim ret As Boolean
ret = PictureBoxSaveJPG(Picture2, CommonDialog1.fileName) '保存压缩后的图片
If ret = False Then
MsgBox "保存失败"
End If
Exit Sub
ErrHandler:
' 用户按了“取消”按钮
Exit Sub
End Sub
热心网友
时间:2022-06-26 22:29
我在VB的主窗体要装入一幅事先选好的图片,程序运行后我可以在图片上进行修改并可以保存它,以被以后调用。请问我怎样办?(CAO) 你可以把Form或PictureBox的AutoDraw设为True,修改后使用VB的SavePicture语句进行存盘。这种存盘的文件为.bmp文件,颜色数与当前屏幕的色彩数相同。如果想转换为其他格式,可以参考下面的相关问题。 如果你的存盘要求很高,例如希望控制存盘文件的色彩、格式,只能借助于其它控件,比如ImageMan( http://www.data-tech.com)、ImagN、Lead Tools( http://www.leadtools.com)等等。
热心网友
时间:2022-06-26 22:29
最好是用VB+Access来实现, 可以保存图片和相应的说明, 还可以分类管理。当然,用VB+MS-SQLserver也可以,但用MS-SQLserver的成本高些。
热心网友
时间:2022-06-26 22:30
有个材料还是什么的,可以用动态的或静态加载