Link to home
Start Free TrialLog in
Avatar of imarshad
imarshadFlag for Pakistan

asked on

Bitmap related.....

Hi all,
         I am developing for Windows CE.NET 4.1 device..... I need to load large Bitmaps(roughly 5000x5000 pixels 4 bit 16 clor image) in my project..... I have done quite a bit of development in eVB and here is how I have managed it....

The following function loads the Bitmap in memory(using CreateCompatibleDC(0)) and returns the hDC of the Memory DC..... Now displaying this image on the screen is really easy as it involves only a call to Bitblt....

    Public Declare Function GetDC Lib "Coredll" (ByVal hWnd As Long) As Long
    Public Declare Function BitBlt Lib "Coredll" (ByVal hDestDC 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 dwRop As Long) As Long
    Public Declare Function SHLoadDIBitmap Lib "Coredll" (ByVal szFileName As String) As Long
    Public Declare Function CreateCompatibleDC Lib "Coredll" (ByVal hdc As Long) As Long
    Public Declare Function DeleteObject Lib "Coredll" (ByVal hObject As Long) As Long
    Public Declare Function SelectObject Lib "Coredll" (ByVal hdc As Long, ByVal hObject As Long) As Long
    Public Declare Function GetLastError Lib "Coredll" () As Long
    Public Declare Function DeleteDC Lib "Coredll.dll" (ByVal hdc As Long) As Long
    Public Declare Function StretchBlt Lib "Coredll.dll" (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
    Public Declare Function CreateCompatibleBitmap Lib "Coredll.dll" (ByVal hdc As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long


    Public Function ImageLoad(ByVal imlHDC As Long, ByVal imlImageName As String) ' Handle to the old bitmap in the DC, for clear up:
        Dim m_hBmpOld As Long  ' Handle to bitmap creating with ImageLoad
        Dim pName As String       'Path to loading image

        pName = imlImageName
         Call DeleteObject(hbmp)
        imlHDC = CreateCompatibleDC(0)
        hbmp = SHLoadDIBitmap(pName)
        If hbmp = 0 Then
            Call DeleteObject(imlHDC)
            imlHDC = 0
            MsgBox("Can't load BMP image" & GetLastError)
            Exit Function
        End If
        m_hBmpOld = SelectObject(imlHDC, hbmp)
        Call DeleteObject(m_hBmpOld)
        ImageLoad = imlHDC
    End Function

This was working quite fine in eVB but I am having quite a big headache in converting it so that it can run in VB.NET compact framework......

Note that I have also tried to use the Bitmap class but I have got no success with it......
Dim flag As New Bitmap("c:\Map.bmp")

creates error(because Map.bmp is about 7 MB) but if I load a small bmp it works OK......

So All I am asking is for a way to load a 7MB BMP into memory DC and get a handle to it so that I can bitblt it on a picture box...... If someone can modify my existing code or give some other suggestions ????

and I am developing for Windows CE.NET i.e using the Compact Framework and I am really a novice in VB.NET development.....

Imran
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

What is the error?  Out of memory, perhaps?

There is a limit to the size of images within GDI+

Bob
Avatar of imarshad

ASKER

I get an ArgumentException "Could not find resource assembly"..
But if I try this with a smaller BMP say 800x600 then it works.... What is wrong?

Imran
What happens if you convert this Bitmap image to JPG?

Bob
>>convert this Bitmap image to JPG?

Not tried so far...... But I think reading a JPG involves more processing then reading a BMP..... And also these BMP's are basically maps of different cities of my area and I have geo-referenced them. So maybe JPG is not that much a good choice...

Anyways have you seen my code that I used in eVB.....Can it be converted to be used with VB.NET Compact Framework???
I somewhere studied that the API's can be imported/used in VB.NET..... How can this be done?
Also I am having one big problem..... In eVB and VB6 we used to have hDC to be used in API's like Bitblt etc. But in VB.NET I have seen Intptr....Whats the difference between them?

Imran
Here's a good explanation of what IntPtr is:

Exploring general functions of the Marshal class:
http://www.codeproject.com/vb/net/Marshal.asp

"IntPtr:  .NET's platform-dependent representation of a memory address"

Getting an HDC using Graphics.GetHdc:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdrawinggraphicsclassgethdctopic.asp

Bob
OK I have found the problem.... The problem was in the declaration of the API's..... In eVB a long was of 32bits while in VB.NET the Long is 64 bits..... So I needed to declare the API's using Integer rather then Long..... Now my image is loading using the function above..... I have one last problem.... Can you tell me how to get the hDC of a picturebox??? I have seen this code on some tutorial

Dim GrpObj as Graphics = PictureBox1.CreateGraphics
Dim MyDeviceContext as IntPtr ' Creating the  variable to keep the DC address
MyDeviceContext = GrpObj.GetHdc()

But this is not working in Compact Framework..... I get error on GrpObj.GetHdc() as there is not GetHdc member for GrpObj..... Can you suggest me how to use Bitblt in VB.NET while blitting on a Picturebox???? or alternativley finding hDC or IntPtr of a picturebox in Compact Framework?????

and as far as the Bitmap class is concerned I think I will post a new question for this.....

Imran
Have you seen this?  Would it help?

Saving a Control Image to a Bitmap File:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetcomp/html/scibf.asp

Bob
I am still lost in this question..... Since I was more busy in learning Oracle and my other project so I wasn't able to reply to this question earlier.... But now I am in need of some solution to this problem..... All I want to know is a procedure to find the hDC of a picturebox in VB.NET compact framework.....  Can you help me or should I ask another question so that more people can also take a look at the question (As this question is no more in the top page)....

Imran
I can't find anything that would be useful.

Bob
Finally I got it.... The key was to first find the hwnd of my form. Here is the code I used

        Me.Capture = True
        Dim hwnd As Integer = GetCapture()
        picDC = GetWindowDC(hwnd, PictureBox1.Top, PictureBox1.Left)
        Me.Capture = False

where GetWindowDC function is declared as

    Function GetWindowDC(ByVal hWnd As Integer, ByVal x As Integer, ByVal y As Integer) As Integer
        Dim ChildWnd As Integer
        ChildWnd = ChildWindowFromPoint(hWnd, x, y)
        GetWindowDC = GetDC(ChildWnd)
    End Function

Imran
ASKER CERTIFIED SOLUTION
Avatar of PAQ_Man
PAQ_Man
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial