如何在VB中使用PNG格式的图片

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在VB中使用PNG格式的图片相关的知识,希望对你有一定的参考价值。

我想在VB中做一个动画效果,可是VB的image控件不支持PNG格式的文件,而用bmp和jpg又没办法去掉图片的白色背景,那位高手帮下忙,告诉我怎样才能在VB中使用PNG的图片格式

  由于VB中的Image控件和Picture控件都不能直接加载PNG格式图片,使用或浏览PNG格式的图片带来了困难。现行的解决方法主要是以下两种:
  (1)利用GDI显示PNG图片;
  (2)将PNG格式图片制成swf格式的文件,再用Flash控件显示。
  第一种方法效果好,但有些复杂,第二种方法不仅复杂,加载的已不是PNG格式图片了。其实PNG格式图片是能直接在WebBrowser控件里显示的,代码如下:view plaincopy to clipboardprint?
  Private Sub Command1_Click()
  On Error GoTo ERRLINE
  CommonDialog1.ShowOpen
  WebBrowser1.Navigate2 CommonDialog1.FileName
  ERRLINE:
  End Sub
  Private Sub Form_Load()
  WebBrowser1.Navigate "about:blank"
  CommonDialog1.Filter = "PNG图片(*.PNG)|*.PNG"
  End Sub
  Private Sub Command1_Click()
  On Error GoTo ERRLINE
  CommonDialog1.ShowOpen
  WebBrowser1.Navigate2 CommonDialog1.FileName
  ERRLINE:
  End Sub
  Private Sub Form_Load()
  WebBrowser1.Navigate "about:blank"
  CommonDialog1.Filter = "PNG图片(*.PNG)|*.PNG"
  End Sub

  这样就可以随意浏览电脑里的PNG格式的图片了。
参考技术A 请你使用GIF控件就行了!PNG转换为GIF格式,这个控件不难找,也许你电脑里就有!或者你用WebBrowse也行不过效果不是很好。本回答被提问者采纳 参考技术B

我这里有个类 LoadPNG

类似的代码如下,仅

Filename = App.Path & "\\test.png"
PicBox.Picture = LoadPicture("")'PicBox是PictureBox
Dim png As New LoadPNG
png.PicBox = PicBox
Test = png.OpenPNG(Filename)
If png.ErrorNumber <> 0 Then MsgBox "Error Nr. " & png.ErrorNumber

类的代码在附件

参考技术C 可以用 GIF 格式呀 参考技术D 用“VB png”搜索一下,网上有源码,我以前也试过

VB如何使用PNG图片

我用了GIF控件,会把PNG图片上的透明状态破坏,把图片改成别的格式也是如此

请问能告诉我像picturebox一样方便的方法吗?
非常感谢!
看了你的问题,你回答说你已经知道像picturebox一样方便的方法了
是怎么弄的啊?或者说把PNG图片怎么处理
反正跟你的问题一样背景要透明
麻烦你告诉我弄下,Thanks.

picturebox控件好像不能显示空白背景(.gif.png等),只有image可以加载gif的图片可以使图片透明。image好像不能加载png的图片。追问

那请问有什么办法加载PNG图片么?我用的是精简版,没有控件!

追答

估计没有

参考技术A API 加载 loadimage 参考技术B PNG貌似不可以,我也没能成功 应用 参考技术C Option Explicit
Private Type GUID
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(0 To 7) As Byte
End Type

Private Type PICTDESC
Size As Long
Type As Long
hPic As Long
hPal As Long
End Type

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 Long
Private Declare Function GdipCreateBitmapFromFile Lib "GDIPlus" (ByVal filename As Long, bitmap As Long) As Long
Private Declare Function GdipCreateHBITMAPFromBitmap Lib "GDIPlus" (ByVal bitmap As Long, _
hbmReturn As Long, ByVal background As Long) As Long
Private Declare Function GdipDisposeImage Lib "GDIPlus" (ByVal image As Long) As Long
Private Declare Function GdiplusShutdown Lib "GDIPlus" (ByVal token As Long) As Long
Private Declare Function OleCreatePictureIndirect Lib "olepro32.dll" (PicDesc As PICTDESC, _
RefIID As GUID, ByVal fPictureOwnsHandle As Long, IPic As IPicture) As Long

Public Function LoadImage(ByVal strFName As String) As IPicture
Dim uGdiInput As GdiplusStartupInput
Dim hGdiPlus As Long
Dim hGdiImage As Long
Dim hBitmap As Long

uGdiInput.GdiplusVersion = 1

If GdiplusStartup(hGdiPlus, uGdiInput) = 0 Then
If GdipCreateBitmapFromFile(StrPtr(strFName), hGdiImage) = 0 Then
GdipCreateHBITMAPFromBitmap hGdiImage, hBitmap, 0
Set LoadImage = ConvertToIPicture(hBitmap)
GdipDisposeImage hGdiImage
End If
GdiplusShutdown hGdiPlus
End If

End Function

Public Function ConvertToIPicture(ByVal hPic As Long) As IPicture

Dim uPicInfo As PICTDESC
Dim IID_IDispatch As GUID
Dim IPic As IPicture

Const PICTYPE_BITMAP = 1

With IID_IDispatch
.Data1 = &H7BF80980
.Data2 = &HBF32
.Data3 = &H101A
.Data4(0) = &H8B
.Data4(1) = &HBB
.Data4(2) = &H0
.Data4(3) = &HAA
.Data4(4) = &H0
.Data4(5) = &H30
.Data4(6) = &HC
.Data4(7) = &HAB
End With

With uPicInfo
.Size = Len(uPicInfo)
.Type = PICTYPE_BITMAP
.hPic = hPic
.hPal = 0
End With

OleCreatePictureIndirect uPicInfo, IID_IDispatch, True, IPic

Set ConvertToIPicture = IPic
End Function

以上是关于如何在VB中使用PNG格式的图片的主要内容,如果未能解决你的问题,请参考以下文章

VB如何使用PNG图片

vb6如何使用png图片

vb6.0 把png格式的图片载入到窗口中。

怎么在VB的图片框里加载PNG格式的图片,或在窗口上显示PNG图片?

怎么在VB的图片框里加载PNG格式的图片,或在窗口上显示PNG图片?

有一张png格式的图片,在delphi中如何把该图片分成三份显示?