Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Bitmap

Status
Not open for further replies.

idinkin

Programmer
Sep 12, 2002
29
0
0
IL
Hi all!
How can I load an image and display it on form using API functions. I mean without using Form1.Picture or ImageBox, PictureBoxes.
Thanx!
 
The code I am pasting here is very simple and brief.
It however works ONLY with hi-color (16-bit) bitmaps and true-color (24-bit) bitmaps. It does not work with bitmaps containing palettes. The code required to handle such bitmaps is a little bit complex and tricky.

---

Private Declare Function CreateBitmap Lib "gdi32" (ByVal nWidth As Long, ByVal nHeight As Long, ByVal nPlanes As Long, ByVal nBitCount As Long, lpBits As Any) As Long
Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
Private Type BITMAPFILEHEADER
bfType As Integer
bfSize As Long
bfReserved1 As Integer
bfReserved2 As Integer
bfOffBits As Long
End Type
Private Type BITMAPINFOHEADER
biSize As Long
biWidth As Long
biHeight As Long
biPlanes As Integer
biBitCount As Integer
biCompression As Long
biSizeImage As Long
biXPelsPerMeter As Long
biYPelsPerMeter As Long
biClrUsed As Long
biClrImportant As Long
End Type

Private Sub Form_Load()
'must supply a hi-color or true-color bitmap
LoadBitmap "C:\Windows\Mandelbrot.bmp"
End Sub

Private Sub LoadBitmap(FileName As String)
Dim bfh As BITMAPFILEHEADER, bih As BITMAPINFOHEADER
Dim FNum As Integer, Bits() As Byte
FNum = FreeFile
Open FileName For Binary As #FNum
Get #FNum, , bfh
Get #FNum, , bih
ReDim Bits(0 To bfh.bfSize - bfh.bfOffBits - 1)
Get #FNum, bfh.bfOffBits + 1, Bits
Close #FNum
hBmp = CreateBitmap(bih.biWidth, bih.biHeight, bih.biPlanes, bih.biBitCount, Bits(0))
AutoRedraw = True
SelectObject hdc, hBmp
End Sub
 
idinkin,

Can you give a brief explanation as to why you want to avoid using "Form1.Picture or ImageBox, PictureBoxes", as it may help to define what sort of solution you actually require. For example, is it merely to avoid immediately displaying the picture when you load it? Is it because you don't have a form to host the controls?
 
I wanna do this coz I make programm a game. And I need a very fast drawing without blinking (with PixtureBoxes it blinks). I need also to aplly Particle Engine, Gradient Filling and it have to be really fast. Can I use the folowing code for this:

Function CreateBitmap(Buffer As ScreenBuffer, DC As Long, ByRef Picture As Bitmap)
ReDim Picture.Data(0 To Picture.BitInfo.bmiHeader.biWidth * Picture.BitInfo.bmiHeader.biHeight * 3 - 1) As Byte
ReDim Picture.BackData(0 To Picture.BitInfo.bmiHeader.biWidth * Picture.BitInfo.bmiHeader.biHeight * 3 - 1) As Byte
GetPictureFromBuffer 0, 0, Picture.BitInfo.bmiHeader.biWidth, _
Picture.BitInfo.bmiHeader.biHeight, Buffer, Picture.BackData
Picture.hBitmap = CreateDIBSection(DC, Picture.BitInfo, DIB_RGB_COLORS, ByVal 0&, ByVal 0&, ByVal 0&)
End Function

Function LoadBitmap(DC As Long, FileName As String, ByRef Picture As Bitmap)
Picture.hBitmap = LoadImage(App.hInstance, FileName, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE)
GetDIBits DC, Picture.hBitmap, 0, Picture.BitInfo.bmiHeader.biHeight, Picture.Data(0), Picture.BitInfo, DIB_RGB_COLORS
End Function

Function DrawPictureToBuffer(X As Long, Y As Long, Buffer As ScreenBuffer, Picture As Bitmap)
Dim Width As Long, Height As Long, BuffWidth As Long, BuffHeight As Long, Pixel As Long, picWidth As Long, picHeight As Long
Dim tempData() As Byte
picWidth = Picture.BitInfo.bmiHeader.biWidth
picHeight = Picture.BitInfo.bmiHeader.biHeight
BuffWidth = Buffer.BitInfo.bmiHeader.biWidth
BuffHeight = Buffer.BitInfo.bmiHeader.biHeight
OutputToBuffer Picture.X, Picture.Y, Buffer, Picture.BackData, picWidth, picHeight
ReDim tempData(0 To UBound(Picture.Data) - 1)
tempData = Picture.Data
Height = Picture.BitInfo.bmiHeader.biHeight
Width = Picture.BitInfo.bmiHeader.biWidth
For k = 0 To picHeight - 1
For i = 0 To picWidth - 1
If tempData((k * picWidth + i) * 3) = 40 And _
tempData((k * picWidth + i) * 3 + 1) = 235 And _
tempData((k * picWidth + i) * 3 + 2) = 200 Then
'if the color is green then change it
Pixel = GetPixel(Buffer.DC, X + i, Y + picHeight - 1 - k)
tempData((k * picWidth + i) * 3) = GetBlue(Pixel)
tempData((k * picWidth + i) * 3 + 1) = GetGreen(Pixel)
tempData((k * picWidth + i) * 3 + 2) = GetRed(Pixel)
End If
Next i
Next k
GetPictureFromBuffer X, Y, picWidth, picHeight, Buffer, Picture.BackData
Call OutputToBuffer(X, Y, Buffer, tempData, picWidth, picHeight)
Picture.X = X
Picture.Y = Y
End Function

Function OutputToBuffer(X As Long, Y As Long, Buffer As ScreenBuffer, Picture() As Byte, picWidth As Long, picHeight As Long)
BuffWidth = Buffer.BitInfo.bmiHeader.biWidth
BuffHeight = Buffer.BitInfo.bmiHeader.biHeight
For k = 0 To picHeight - 1
For i = 0 To picWidth - 1
Buffer.Data(((Y + k) * BuffWidth + X + i) * 3) = _
Picture((k * picWidth + i) * 3)
Buffer.Data(((Y + k) * BuffWidth + X + i) * 3 + 1) = _
Picture((k * picWidth + i) * 3 + 1)
Buffer.Data(((Y + k) * BuffWidth + X + i) * 3 + 2) = _
Picture((k * picWidth + i) * 3 + 2)
Next i
Next k
End Function

Thanks!
 
Fine. I'll just point out that the basic LoadPicture will do all the above somewhat faster than your code. Now, once you have got the picture loaded, and can treat it as a bitmap (you can get the hBmp from the StdPicture/IPictureDisp's handle property), and do what you like with it; LoadPicture does NOT need to be focused on a PictureBox control...

And your DrawPicture function can almost certainly be replaced by VB's PaintPicture, or the API's BitBlt (or related function)
 
You can also avoid flickering or blinking of bitmap by setting the autoredraw property of your picture box to true.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top