Как загрузить рисунок PNG на форму - VB

Узнай цену своей работы

Формулировка задачи:

Такой вопрос, как заставить Image или PictureBox понимать формат рисунка .PNG кто сталкивался помогите пожалуйста.

Решение задачи: «Как загрузить рисунок PNG на форму»

textual
Листинг программы
  1. Private Declare Function StretchBlt Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal nSrcWidth As Long, ByVal nSrcHeight As Long, ByVal dwRop As Long) As Long
  2. Private Enum GpStatus
  3.     Ok = 0
  4.     GenericError = 1
  5.     InvalidParameter = 2
  6.     OutOfMemory = 3
  7.     ObjectBusy = 4
  8.     InsufficientBuffer = 5
  9.     NotImplemented = 6
  10.     Win32Error = 7
  11.     WrongState = 8
  12.     Aborted = 9
  13.     FileNotFound = 10
  14.     ValueOverflow = 11
  15.     AccessDenied = 12
  16.     UnknownImageFormat = 13
  17.     FontFamilyNotFound = 14
  18.     FontStyleNotFound = 15
  19.     NotTrueTypeFont = 16
  20.     UnsupportedGdiplusVersion = 17
  21.     GdiplusNotInitialized = 18
  22.     PropertyNotFound = 19
  23.     PropertyNotSupported = 20
  24. End Enum
  25.  
  26. Private Type GdiplusStartupInput
  27.     GdiplusVersion As Long
  28.     DebugEventCallback As Long
  29.     SuppressBackgroundThread As Long
  30.     SuppressExternalCodecs As Long
  31. End Type
  32.  
  33. Private Declare Function GdipDrawImageRect Lib "gdiplus" (ByVal graphics As Long, ByVal image As Long, ByVal x As Single, ByVal y As Single, ByVal Width As Single, ByVal Height As Single) As GpStatus
  34. Private Declare Function GdipLoadImageFromFile Lib "gdiplus" (ByVal FileName As String, image As Long) As GpStatus
  35. Private Declare Function GdipGetImageWidth Lib "gdiplus" (ByVal image As Long, Width As Long) As GpStatus
  36. Private Declare Function GdipGetImageHeight Lib "gdiplus" (ByVal image As Long, Height As Long) As GpStatus
  37. Private Declare Function GdipDisposeImage Lib "gdiplus" (ByVal image As Long) As GpStatus
  38. Private Declare Function GdiplusStartup Lib "gdiplus" (token As Long, inputbuf As GdiplusStartupInput, Optional ByVal outputbuf As Long = 0) As GpStatus
  39. Private Declare Function GdipCreateFromHDC Lib "gdiplus" (ByVal hdc As Long, graphics As Long) As GpStatus
  40. Private Declare Function GdipDeleteGraphics Lib "gdiplus" (ByVal graphics As Long) As GpStatus
  41.  
  42. Private token As Long
  43.  
  44. Private Function LoadImage(ByVal FileName As String, ByVal hDestDC As Long) As GpStatus
  45.     Dim lngWidth As Long, lngHeight As Long
  46.     Dim graphics As Long, img As Long
  47.     Dim GpInput As GdiplusStartupInput
  48.     Dim tmp_status As GpStatus
  49.    
  50.     GpInput.GdiplusVersion = 1
  51.     tmp_status = GdiplusStartup(token, GpInput)
  52.         If tmp_status <> Ok Then GoTo The_End:
  53.     tmp_status = GdipCreateFromHDC(hDestDC, graphics)
  54.         If tmp_status <> Ok Then GoTo The_End:
  55.     tmp_status = GdipLoadImageFromFile(StrConv(FileName, vbUnicode), img)
  56.         If tmp_status <> Ok Then GoTo The_End:
  57.     tmp_status = GdipGetImageHeight(img, lngHeight)
  58.         If tmp_status <> Ok Then GoTo The_End:
  59.     tmp_status = GdipGetImageWidth(img, lngWidth)
  60.         If tmp_status <> Ok Then GoTo The_End:
  61.     tmp_status = GdipDrawImageRect(graphics, img, 0, 0, lngWidth, lngHeight)
  62.         If tmp_status <> Ok Then GoTo The_End:
  63.     tmp_status = GdipDisposeImage(img)
  64.         If tmp_status <> Ok Then GoTo The_End:
  65.     tmp_status = GdipDeleteGraphics(graphics)
  66. The_End:
  67.     LoadImage = tmp_status
  68. End Function
  69.  
  70.  
  71. Private Sub Command1_Click()
  72. LoadImage "C:\mspaint.png", Picture1.hdc
  73. End Sub
  74.  
  75. Private Sub Form_Load()
  76.     Me.AutoRedraw = True
  77.     'LoadImage "C:\mspaint.png", Me.hdc
  78.    Me.Refresh
  79. End Sub

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

15   голосов , оценка 4.067 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы