十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
VB6.0可使用SavePicture 语句来保存窗体或图像框中经修改的图片。
创新互联是专业的北碚网站建设公司,北碚接单;提供做网站、成都网站设计,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行北碚网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!
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
将PictureBox控件里的图片,保存为文件:
1,文件格式不变化:
PictureBox1.Image.Save("C:\" Format(Now, "HH-mm-ss") ".bmp")
2,文件格式有变化:
PictureBox1.Image.Save("C:\" Format(Now, "HH-mm-ss") ".bmp", System.Drawing.Imaging.ImageFormat.Bmp)
我理解你
我写过图片网络传输的和你要求的一样
为了不把图片写到硬盘 又从硬盘读取
而直接保存图片内存流 进行网络传输
从网络读取的图片byte数组转成图片代码如下
Dim mStream As New IO.MemoryStream
mStream.Write(b, 0, b.length) '这里b就是你的一维数组了
Dim Img As New Bitmap(mStream)
mStream.Close()
mStream.Dispose()
下面是把图片保存到一维数组的方法
Dim Stream As New IO.MemoryStream
im.Save(Stream, System.Drawing.Imaging.ImageFormat.Jpeg)
im = Nothing
Stream.Flush()
Dim b As Byte() = Stream.ToArray
Stream.Dispose()
Dim bitmap1 As New Bitmap("D:\image\0183.jpg")‘panel1.backgroundimage
Dim bitmap2 As New Bitmap(294, 294)’保存图,也就是绘制的大小。
Dim rect1 As New Rectangle(New Point, bitmap2.Size)‘昨天的问题,这里似乎写错了,当然也不算错,只是把新图(小图)扩展成原图大小(大图),这里改正为新图的原大小。
Dim rect2 As New Rectangle(New Point(6, 6), bitmap2.Size)
Dim g As Graphics = Graphics.FromImage(bitmap2)
e.Graphics.DrawImage(bitmap1, rect1, rect2, GraphicsUnit.Pixel)
g.DrawImage(bitmap1, rect1, rect2, GraphicsUnit.Pixel)
bitmap2.Save("d:\a.png", Drawing.Imaging.ImageFormat.Png)