Skip to content

IMAGECREATE


为图像分配并初始化存储空间

语法

vb
declare function ImageCreate ( byval width as long, byval height as long, byval color as ulong = transparent_color ) as any ptr
declare function ImageCreate ( byval width as long, byval height as long, byval color as ulong = transparent_color, byval depth as long ) as any ptr

用法

result = ImageCreate( width, height [, [ color ][, depth ]] )

参数

width

所需宽度,以像素为单位。

height

所需高度,以像素为单位。

color

用于填充图像区域的像素值。

depth

所需颜色深度,以每像素位数为单位。

返回值

如果无法创建图像,则返回 NULL(0),否则返回图像的地址。ImageCreate 必须在图形模式初始化后调用,否则返回 0

因此,在 Shared 变量声明的情况下,ImageCreate 不能用作集成初始化器,即使在 UDT 内部(成员字段或构造函数中),因为(共享变量的)初始化值在任何用户代码运行之前的程序启动时就已设置。图像分配调用必须是独立的可执行指令,并在图形模式初始化之后。

说明

两个过程都为指定 widthheight 的图像分配内存。如果不成功,则返回 NULL(0)。否则,创建该大小的图像,并用值 color 填充整个像素区域来初始化。如果未指定,color 假设为当前图形屏幕的透明颜色值,可通过调用 ScreenControl 获得。无论如何,都返回图像的地址,该地址由用户控制,必须使用 ImageDestroy 销毁。

第一个过程创建颜色深度与当前图形屏幕匹配的图像,可通过调用 ScreenControl 获得。第二个过程创建颜色深度为 depth(以每像素位数为单位)的图像。对于两个过程,只要图像的颜色深度与图形屏幕的颜色深度匹配,生成的图像就可以在任何屏幕模式下——以及跨模式切换——用于绘图过程。

ImageCreate 是为新图像分配内存的推荐方式。内存布局——大小、结构等——虽然有文档记录,但可能随版本变化,使手动计算所涉及大小容易出错。但是,可以使用 ImageInfo 检索现有图像的大小(以字节为单位),从而允许手动为图像副本分配内存,或从文件或设备读取/写入。

Get (Graphics) 可用于使用预先分配的内存初始化图像。

示例

start GeSHi

vb
'' Create a graphics screen.
ScreenRes 320, 200, 32

'' Create a 64x64 pixel image with a darkish green background.
Dim image As Any Ptr = ImageCreate( 64, 64, RGB(0, 128, 0) )

If image = 0 Then
    Print "Failed to create image."
    Sleep
    End -1
End If

'' Draw a semi-transparent, red circle in the center of the image.
Circle image, (32, 32), 28, RGBA(255, 0, 0, 128),,, 1.0, f

'' Draw the image onto the screen using various blitting methods.
Put (120, 60), image, PSet
Put (140, 80), image, Alpha

'' Destroy the image.
ImageDestroy image

Sleep

end GeSHi

方言差异

  • -lang qb 方言中不可用,除非使用别名 __Imagecreate 引用。

与 QB 的区别

  • FreeBASIC 新增

另请参阅

返回 目录

基于 FreeBASIC 官方文档翻译 如有侵权请联系我们删除
FreeBASIC 是开源项目,与微软公司无隶属关系