VB利用GDI+显示资源文件中的PNG文件

'加载并打开资源文件,点击"添加自定义资源",添加一个PNG文件,保存。
'在代码编辑窗口复制粘贴以下代码,按F5运行后鼠标点击窗口即可显示资源文件中的PNG文件
在这里插入图片描述

Option Explicit
Private Enum GpStatus
Ok = 0
GenericError = 1
InvalidParameter = 2
OutOfMemory = 3
ObjectBusy = 4
InsufficientBuffer = 5
NotImplemented = 6
Win32Error = 7
WrongState = 8
Aborted = 9
FileNotFound = 10
ValueOverflow = 11
AccessDenied = 12
UnknownImageFormat = 13
FontFamilyNotFound = 14
FontStyleNotFound = 15
NotTrueTypeFont = 16
UnsupportedGdiplusVersion = 17
GdiplusNotInitialized = 18
PropertyNotFound = 19
PropertyNotSupported = 20
End Enum
Private Enum QualityMode
QualityModeInvalid = -1
QualityModeDefault = 0
QualityModeLow = 1
QualityModeHigh = 2
End Enum
Private Enum SmoothingMode
SmoothingModeInvalid = QualityModeInvalid
SmoothingModeDefault = QualityModeDefault
SmoothingModeHighSpeed = QualityModeLow
SmoothingModeHighQuality = QualityModeHigh
SmoothingModeNone
SmoothingModeAntiAlias
End Enum
Private Type GdiplusStartupInput
GdiplusVersion As Long
DebugEventCallback As Long
SuppressBackgroundThread As Long
SuppressExternalCodecs As Long
End Type
Private Declare Function GdiplusStartup Lib “gdiplus” (Token As Long, inputbuf As GdiplusStartupInput, Optional ByVal outputbuf As Long = 0) As GpStatus
Private Declare Function GdiplusShutdown Lib “gdiplus” (ByVal Token As Long) As GpStatus
Private Declare Sub CreateStreamOnHGlobal Lib “ole32.dll” (ByRef hGlobal As Any, ByVal fDeleteOnRelease As Long, ByRef ppstm As Any)
Private Declare Function GdipLoadImageFromStream Lib “gdiplus” (ByVal Stream As Any, ByRef Image As Long) As Long
Private Declare Function GdipGetImageWidth Lib “gdiplus” (ByVal Image As Long, Width As Long) As GpStatus
Private Declare Function GdipGetImageHeight Lib “gdiplus” (ByVal Image As Long, Height As Long) As GpStatus
Private Declare Function GdipCreateFromHDC Lib “gdiplus” (ByVal hdc As Long, Graphics As Long) As GpStatus
Private Declare Function GdipSetSmoothingMode Lib “gdiplus” (ByVal Graphics As Long, ByVal SmoothingMd As Long) As GpStatus
Private Declare Function GdipDrawImageRectRectI Lib “gdiplus” (ByVal Graphics As Long, ByVal hImage As Long, ByVal dstx As Long, ByVal dsty As Long, ByVal dstwidth As Long, ByVal dstheight As Long, ByVal srcx As Long, ByVal srcy As Long, ByVal srcwidth As Long, ByVal srcheight As Long, ByVal srcUnit As Long, Optional ByVal imageAttributes As Long = 0, Optional ByVal callback As Long = 0, Optional ByVal callbackData As Long = 0) As Long
Private Declare Function GdipDisposeImage Lib “gdiplus” (ByVal Image As Long) As GpStatus
Private Declare Function GdipDeleteGraphics Lib “gdiplus” (ByVal Graphics As Long) As GpStatus
Private Const UnitPixel = 2

'开启/关闭GDI+
Private Function SwitchGDI(ByVal On_Off As Boolean) As GpStatus
Dim Token As Long, gdipInit As GdiplusStartupInput
Select Case On_Off
Case True
gdipInit.GdiplusVersion = 1
SwitchGDI = GdiplusStartup(Token, gdipInit, ByVal 0&)
Case False
GdiplusShutdown Token
End Select
End Function

'显示资源文件中的PNG(场景DC,左偏移X值,右偏移Y值,显示局部宽度,显示局部高度,资源文件的ID号,类型)
Private Function LoadPng(ByVal DC As Long, ByVal X1 As Long, ByVal Y1 As Long, ByVal W1 As Long, ByVal H1 As Long, ByVal ImgID As Long, ByVal FileType As String) As Long
'参数是资源ID和类型
Dim Pimg As Long '图像句柄
Dim ImgW As Long, ImgH As Long '图像的宽度高度
Dim Graphics As Long
Dim ResData() As Byte, Stream As Object
On Error GoTo Nx
'加载资源文件到一个byte变量数组中
ResData = LoadResData(ImgID, FileType)
'从句柄中获取信息并保存在内存中进行处理
Call CreateStreamOnHGlobal(ResData(0), False, Stream)
'根据文件创建图片对象–流 Stream
Call GdipLoadImageFromStream(Stream, Pimg)
'获得图像宽度
Call GdipGetImageWidth(Pimg, ImgW)
'获得图像高度
Call GdipGetImageHeight(Pimg, ImgH)
'创建与指定设备上下文相关联的图像对象(Graphics)显示到设备上
Call GdipCreateFromHDC(Me.hdc, Graphics)
'设置图像对象的渲染质量(反锯齿)
Call GdipSetSmoothingMode(Graphics, SmoothingModeNone)
'在指定位置绘制图像。
Call GdipDrawImageRectRectI(Graphics, Pimg, X1, X1, W1, H1, 0, 0, ImgW, ImgH, UnitPixel, 0, 0, 0)
LoadPng = 1
Call GdipDisposeImage(Pimg)
Call GdipDeleteGraphics(Graphics)
Exit Function
Nx:
LoadPng = 0
Set Stream = Nothing
End Function

'鼠标点击窗口显示PNG
Private Sub Form_Click()
Me.Cls
Call LoadPng(Me.hdc, 0, 0, Me.ScaleWidth, Me.ScaleHeight, 101, “CUSTOM”)
Me.Refresh
End Sub

'初始化
Private Sub Form_Load()
Me.AutoRedraw = True
Me.ScaleMode = vbPixels
Call SwitchGDI(True) '初始化GDI+
End Sub

'退出窗口
Private Sub Form_Unload(Cancel As Integer)
Call SwitchGDI(False) '关闭GDI+
End Sub

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/55727.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【雕爷学编程】Arduino动手做(184)---快餐盒盖,极低成本搭建机器人实验平台2

吃完快餐粥,除了粥的味道不错之外,我对个快餐盒的圆盖子产生了兴趣,能否做个极低成本的简易机器人呢?也许只需要二十元左右 知识点:轮子(wheel) 中国词语。是用不同材料制成的圆形滚动物体。简…

大数据学习教程:Linux 高级教程(上)

一、Linux用户与权限 1. 用户和权限的基本概念 1.1、基本概念 用户 是Linux系统工作中重要的一环, 用户管理包括 用户 与 组 管理 在Linux系统中, 不论是由本级或是远程登录系统, 每个系统都必须拥有一个账号, 并且对于不同的系统资源拥有不同的使用权限 对 文件 / 目录 的…

软件测试分类总结

目录 1.根据源代码可见度划分 1.1黑盒测试 1.2白盒测试 1.3灰盒测试 2.根据开发阶段划分 2.1单元测试 2.2集成测试 2.3系统测试 2.4验收测试 3.按照实施组织划分 3.1α测试 3.2β测试 3.3第三方测试 4.按照是否运行程序划分 4.1静态测试 4.2动态测试 5.根据软件测试工作的…

win10配置rocketmq

下载地址:下载 | RocketMQ ,版本根据自己情况定,我选用的是4.7.* 1、下载后解压。D:\rocketmq-all-4.7.0-bin-release 2、需要注意一下jdk安装目录的目录名不能有空格,否则会报错找不到JAVA 3、环境变量新增ROCKEMQ_THOMED:\rocketmq-all-4…

API接口统一管理

API接口统一管理 在开发项目的时候,接口可能很多需要统一管理。在src目录下去创建api文件夹去统一管理项目的接口;这样便于后期维护和团队开发。 axios二次封装 对于axios不熟悉的话,建议先学习这篇文章:Axios的基本使用 在开发项目的时候避免不了与后…

EditPlus取消自动.bak备份

Tools->Preferences->File 将√取消

NetSuite 2023.2 Cash 360 功能更新

大约一年前,Cash 360功能推出。我们写了篇介绍: NetSuite Cash 360_netsuite oneworld数据可以迁移到sap上吗_NetSuite知识会的博客-CSDN博客Cash 360是在SuiteWorld 2021做的预告,本来是要跟着22.1发布出来,但是各种原因导致跳票…

NXP让位!同比增长近3倍!高通领跑「智能座舱」背后的新变局

智能座舱的持续“火爆”,也带动主流芯片一路向上,背后的市场格局也处于剧烈变化的新周期。 高工智能汽车研究院监测数据显示,2023年1-6月中国市场(不含进出口)乘用车前装标配8155芯片搭载交付新车64.91万辆&#xff0…

RPC框架引入zookeeper服务注册与服务发现

Zookeeper概念及其作用 ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是大数据生态中的重要组件。它是集群的管理者,监视着集群中各个节点的状态根据节点提交的反馈进行下一步合理…

python-MySQL数据库建表语句(需要连接数据库)转存为Excel文档-工作小记

将create table XXXXXX 转为指定Excel文档。该脚本适用于数据库表结构本地文档记录 呈现效果 代码 # -*- coding:utf-8 -*- # Time : 2023/8/2 15:14 # Author: 水兵没月 # File : MySQL建表_2_excel.py import reimport mysql.connector import pandas as pd db 库名 mydb …

Java自定义校验注解实现List、set集合字段唯一性校验

文章目录 一: 使用场景二: 定义FieldUniqueValid注解2.1 FieldUniqueValid2.2 注解说明2.3 Constraint 注解介绍2.4 FieldUniqueValid注解使用 三:自定义FieldUniqueValidator校验类3.1 实现ConstraintValidator3.2 重写initialize方法3.3 重…

线性规划和单纯形法-原理篇

文章目录 引言线性规划标准型问题特点单纯形法 引言 很多运筹学的教材都是从线性规划开始的,我平时做算法策略的落地应用时也研发了一部分基于线性规划的技术方案。可以说,如果搞不懂线性规划,很难成为一名优秀的运筹优化算法工程师。 但是…