Option Explicit

 Private Const UnitPixel                  As Long =
Private Const EncoderQuality As String = "{1D5BE4B5-FA4A-452D-9CDD-5DB35105E7EB}" Private Type GdiplusStartupInput
GdiplusVersion As Long
DebugEventCallback As Long
SuppressBackgroundThread As Long
SuppressExternalCodecs As Long
End Type Private Enum EncoderParameterValueType
EncoderParameterValueTypeByte =
EncoderParameterValueTypeASCII =
EncoderParameterValueTypeShort =
EncoderParameterValueTypeLong =
EncoderParameterValueTypeRational =
EncoderParameterValueTypeLongRange =
EncoderParameterValueTypeUndefined =
EncoderParameterValueTypeRationalRange =
End Enum Private Type EncoderParameter
GUID( To ) As Long
NumberOfValues As Long
Type As EncoderParameterValueType
Value As Long
End Type Private Type EncoderParameters
Count As Long
Parameter As EncoderParameter
End Type Private Type ImageCodecInfo
ClassID( To ) As Long
FormatID( To ) As Long
CodecName As Long
DllName As Long
FormatDescription As Long
FilenameExtension As Long
MimeType As Long
Flags As Long
Version As Long
SigCount As Long
SigSize As Long
SigPattern As Long
SigMask As Long
End Type Private Declare Function GdiplusStartup Lib "gdiplus" (Token As Long, inputbuf As GdiplusStartupInput, Optional ByVal outputbuf As Long = ) As Long
Private Declare Sub GdiplusShutdown Lib "gdiplus" (ByVal Token As Long)
Private Declare Function GdipSaveImageToFile Lib "gdiplus" (ByVal hImage As Long, ByVal sFilename As Long, clsidEncoder As Any, encoderParams As Any) As Long
Private Declare Function GdipDisposeImage Lib "gdiplus" (ByVal Image As Long) As Long
Private Declare Function GdipCreateBitmapFromHBITMAP Lib "gdiplus" (ByVal hbm As Long, ByVal hPal As Long, Bitmap As Long) As Long
Private Declare Function GdipGetImageEncodersSize Lib "gdiplus" (numEncoders As Long, Size As Long) As Long
Private Declare Function GdipGetImageEncoders Lib "gdiplus" (ByVal numEncoders As Long, ByVal Size As Long, Encoders As Any) As Long Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Declare Function lstrlenW Lib "kernel32" (ByVal psString As Any) As Long
Private Declare Function CLSIDFromString Lib "ole32" (ByVal lpszProgID As Long, pCLSID As Any) As Long
Private Declare Function GdipBitmapSetResolution Lib "gdiplus" (ByVal Bitmap As Long, ByVal xdpi As Single, ByVal ydpi As Single) As Long Public Enum ImageFileFormat
Bmp =
Jpg =
Png =
Gif =
End Enum Public Function SaveStdPicToFile(Stdpic As StdPicture, ByVal FileName As String, _
Optional ByVal FileFormat As ImageFileFormat = Jpg, _
Optional ByVal JpgQuality As Long = , _
Optional Resolution As Single) As Boolean Dim CLSID() As Long
Dim Bitmap As Long
Dim Token As Long
Dim Gsp As GdiplusStartupInput Gsp.GdiplusVersion = 'GDI+ 1.0版本
GdiplusStartup Token, Gsp '初始化GDI+
GdipCreateBitmapFromHBITMAP Stdpic.Handle, Stdpic.hPal, Bitmap
If Bitmap <> Then '说明我们成功的将StdPic对象转换为GDI+的Bitmap对象了
GdipBitmapSetResolution Bitmap, Resolution, Resolution
Select Case FileFormat
Case ImageFileFormat.Bmp
If Not GetEncoderClsID("Image/bmp", CLSID) = - Then
SaveStdPicToFile = (GdipSaveImageToFile(Bitmap, StrPtr(FileName), CLSID(), ByVal ) = )
End If
Case ImageFileFormat.Jpg 'JPG格式可以设置保存的质量
Dim aEncParams() As Byte
Dim uEncParams As EncoderParameters
If GetEncoderClsID("Image/jpeg", CLSID) <> - Then
uEncParams.Count = ' 设置自定义的编码参数,这里为1个参数
If JpgQuality < Then
JpgQuality =
ElseIf JpgQuality > Then
JpgQuality =
End If
ReDim aEncParams( To Len(uEncParams))
With uEncParams.Parameter
.NumberOfValues =
.Type = EncoderParameterValueTypeLong ' 设置参数值的数据类型为长整型
Call CLSIDFromString(StrPtr(EncoderQuality), .GUID()) ' 设置参数唯一标志的GUID,这里为编码品质
.Value = VarPtr(JpgQuality) ' 设置参数的值:品质等级,最高为100,图像文件大小与品质成正比
End With
CopyMemory aEncParams(), uEncParams, Len(uEncParams)
SaveStdPicToFile = (GdipSaveImageToFile(Bitmap, StrPtr(FileName), CLSID(), aEncParams()) = )
End If
Case ImageFileFormat.Png
If Not GetEncoderClsID("Image/png", CLSID) = - Then
SaveStdPicToFile = (GdipSaveImageToFile(Bitmap, StrPtr(FileName), CLSID(), ByVal ) = )
End If
Case ImageFileFormat.Gif
If Not GetEncoderClsID("Image/gif", CLSID) = - Then '如果原始的图像是24位,则这个函数会调用系统的调色板来将图像转换为8位,转换的效果会不尽人意,但也有可能系统不自动转换,保存失败
SaveStdPicToFile = (GdipSaveImageToFile(Bitmap, StrPtr(FileName), CLSID(), ByVal ) = )
End If
End Select
End If
GdipDisposeImage Bitmap '注意释放资源
GdiplusShutdown Token '关闭GDI+。
End Function Private Function GetEncoderClsID(strMimeType As String, ClassID() As Long) As Long
Dim Num As Long
Dim Size As Long
Dim I As Long
Dim Info() As ImageCodecInfo
Dim Buffer() As Byte
GetEncoderClsID = -
GdipGetImageEncodersSize Num, Size '得到解码器数组的大小
If Size <> Then
ReDim Info( To Num) As ImageCodecInfo '给数组动态分配内存
ReDim Buffer( To Size) As Byte
GdipGetImageEncoders Num, Size, Buffer() '得到数组和字符数据
CopyMemory Info(), Buffer(), (Len(Info()) * Num) '复制类头
For I = To Num '循环检测所有解码
If (StrComp(PtrToStrW(Info(I).MimeType), strMimeType, vbTextCompare) = ) Then '必须把指针转换成可用的字符
CopyMemory ClassID(), Info(I).ClassID(), '保存类的ID
GetEncoderClsID = I '返回成功的索引值
Exit For
End If
Next
End If
End Function Private Function PtrToStrW(ByVal lpsz As Long) As String
Dim Out As String
Dim Length As Long
Length = lstrlenW(lpsz)
If Length > Then
Out = StrConv(String$(Length, vbNullChar), vbUnicode)
CopyMemory ByVal Out, ByVal lpsz, Length *
PtrToStrW = StrConv(Out, vbFromUnicode)
End If
End Function

GDI+_SavePic的更多相关文章

  1. 超全面的.NET GDI+图形图像编程教程

    本篇主题内容是.NET GDI+图形图像编程系列的教程,不要被这个滚动条吓到,为了查找方便,我没有分开写,上面加了目录了,而且很多都是源码和图片~ (*^_^*) 本人也为了学习深刻,另一方面也是为了 ...

  2. (转载)GDI+双缓冲

    双缓冲在GDI+里可以有效的提高描画效率.改善显示的质量. 下面的代码是一个最简单的双缓冲的模板.可以根据需要,做简单的修改即可. Bitmap CacheImage( [Width], [Heigh ...

  3. (转载)解决GDI闪烁

    一般的windows 复杂的界面需要使用多层窗口而且要用贴图来美化,所以不可避免在窗口移动或者改变大小的时候出现闪烁. 先来谈谈闪烁产生的原因 原因一:如果熟悉显卡原理的话,调用GDI函数向屏幕输出的 ...

  4. 通过GDI+绘制 验证码

    只为了记录下自己的学习历程,方便日后查看 现在开始言归正传,以下为其完整代码附上 using System; using System.Collections.Generic; using Syste ...

  5. 【VC++技术杂谈007】使用GDI+进行图片格式转换

    本文主要介绍如何使用GDI+对图片进行格式转换,可以转换的图片格式为bmp.jpg.png. 1.加载GDI+库 GDI+是GDI图形库的一个增强版本,提供了一系列Visual C++ API.为了使 ...

  6. C# GDI绘制矩形框,鼠标左键拖动可移动矩形框,滚轮放大缩小矩形框

    最近工作需要,要做一个矩形框,并且 用鼠标左键拖动矩形框移动其位置.网上查了一些感觉他们做的挺复杂的.我自己研究一天,做了一个比较简单的,发表出来供大家参考一下.如觉得简单,可路过,谢谢.哈哈. 先大 ...

  7. 【Windows编程】系列第五篇:GDI图形绘制

    上两篇我们学习了文本字符输出以及Unicode编写程序,知道如何用常见Win32输出文本字符串,这一篇我们来学习Windows编程中另一个非常重要的部分GDI图形绘图.Windows的GDI函数包含数 ...

  8. GDI+ 笔记

    1.GDI+模板 #include<windows.h> #include<GdiPlus.h> #include <time.h> #include <ma ...

  9. C# GDI+发生一般性错误(A generic error occurred in GDI+))

    解决思路: 1. 因为 .net GDI+ 是对底层 的封装. 所以可以尝试用 Marshal.GetLastWin32Error();函数获得底层错误代码. try{ image.Save(file ...

随机推荐

  1. Font 'C:\WINDOWS\FONTS\msyh.ttc' with 'Identity-H' is not recognized

    在生成PDF使用windows自带字体雅黑的时候不停的报“Font 'C:\WINDOWS\FONTS\msyh.ttc' with 'Identity-H' is not recognized”的错 ...

  2. Linux系统下用find命令查找最近修改过的文件

    1.先进入指定目录,比如:cd /var/ftp/ss 2.输入指定命令,相关命令如下介绍: 最近24小时内修改过的文件 find ./ -mtime 0 前48~24小时修改过的文件 find ./ ...

  3. 关于loadrunner的了解

    1.性能测试目的: 为什么要进行性能测试呢?  有些问题是只有在大并发或者压力测试下才会暴露出来的,在平常的公司内部测试中,感觉一切都是正常的,但是把服务放到生产线上,例如某个时刻突然有很多的用户要向 ...

  4. spring jpa + mybatis快速开始:

    springmvc开始搭建 源码地址 https://gitee.com/flydb/spingjpamy pom: <packaging>war</packaging> &l ...

  5. 【java多线程】队列系统之DelayQueue源码

    一.延迟队列 延迟队列,底层依赖了优先级队列PriorityBlockingQueue 二.延迟队列案例 (1)延迟队列的任务 public class DelayTask implements De ...

  6. UltraISO 9.7.1.3519注册码

    王涛 7C81-1689-4046-626F redcaps 82C6-3DEF-AB07-0EC0

  7. SpringBoot配置(1) 配置文件application&yml

    SpringBoot配置(1) 配置文件application&yml 一.配置文件 1.1 配置文件 SpringBoot使用一个全局的配置文件,配置文件名是固定的. application ...

  8. mysql识别中文

    在配置的INI中加上这些 [mysql]default-character-set=utf8no-auto-rehash# Remove the next comment character if y ...

  9. 前端-JavaScript1-1——JavaScript简介

    1.1 JavaScript用途 前端三层: 结构层   HTML           从语义的角度描述页面的结构 样式层   CSS               从审美的角度装饰页面 行为层   J ...

  10. 在Docker中监控Java应用程序的5个方法

    译者注:Docker是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器上,也可以实现虚拟化.通常情况下,监控的主要目的在于:减少宕机 ...