473,574 Members | 3,011 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Drawing on a .Net device context from a vb6 dll

I have a vb6 application. On the main form is a picture
box with one or two images and several pieces of text
displayed in it. These are created on the fly using gdi32
routines that are all in a referenced, custom dll. I call
a PrintImage routine in the dll and pass it only the the
Picturebox.hdc from the main form. The dll's print routine
draws to the hdc and it shows up in the picturebox
perfectly.

I would like to do a similar thing with an asp.net page
using my same vb6 custom printing dll. Where Display.aspx
has and IMG tag with src="Image.aspx ". Now, in the
Image.aspx.vb code, I make the necessary calls to my dll
to preload some data, then I call the PrintImage routine.
What I get back is a black box. Since it's an asp.net page
I don't have a picturebox to draw to (even if I did, I'm
not sure the picturebox's Handle would be the same as
vb6's hdc). So I'm trying to create a bitmap in .net where
i can eventually get an hdc that I can send to my printing
routine in the vb6 dll. Below is the code I use to do this:

Dim bm As Bitmap = New Bitmap(728, 312)
Dim gfx As Graphics = Graphics.FromIm age(bm)
gfx.Clear(Color .White)
Dim hDC As IntPtr = gfx.GetHdc()
prt.hdc = hDC.ToInt32 'prt.hdc is the property in my vb6
dll that receives the hdc to print to

The code runs without error, I save the bitmap to a file,
read it into a bytearray, and do a
Response.Binary write...I get a black box (well, now I get
a white box since I added the gfx.Clear line above).

Why can't my vb6 printing dll draw to a .net hdc?

PS. I eventually don't want to have to save the bitmap to
a file. If I can get the correct drawing on a device
context, I'd like to be able to stream that directly to a
byte array and do a Response.Binary write.

If an expert can help with my main issue and my PS. I
would greatly appreciate it.
Nov 20 '05 #1
13 3302
Hi,

You can try calling ReleaseDC after the VB6 dll has finished drawing. The
rest of .NET code looks correct. You might also want to debug your VB6 dll
to see where it possibly fails.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Metallicra ft" <an*******@disc ussions.microso ft.com> wrote in message
news:06******** *************** *****@phx.gbl.. .
I have a vb6 application. On the main form is a picture
box with one or two images and several pieces of text
displayed in it. These are created on the fly using gdi32
routines that are all in a referenced, custom dll. I call
a PrintImage routine in the dll and pass it only the the
Picturebox.hdc from the main form. The dll's print routine
draws to the hdc and it shows up in the picturebox
perfectly.

I would like to do a similar thing with an asp.net page
using my same vb6 custom printing dll. Where Display.aspx
has and IMG tag with src="Image.aspx ". Now, in the
Image.aspx.vb code, I make the necessary calls to my dll
to preload some data, then I call the PrintImage routine.
What I get back is a black box. Since it's an asp.net page
I don't have a picturebox to draw to (even if I did, I'm
not sure the picturebox's Handle would be the same as
vb6's hdc). So I'm trying to create a bitmap in .net where
i can eventually get an hdc that I can send to my printing
routine in the vb6 dll. Below is the code I use to do this:

Dim bm As Bitmap = New Bitmap(728, 312)
Dim gfx As Graphics = Graphics.FromIm age(bm)
gfx.Clear(Color .White)
Dim hDC As IntPtr = gfx.GetHdc()
prt.hdc = hDC.ToInt32 'prt.hdc is the property in my vb6
dll that receives the hdc to print to

The code runs without error, I save the bitmap to a file,
read it into a bytearray, and do a
Response.Binary write...I get a black box (well, now I get
a white box since I added the gfx.Clear line above).

Why can't my vb6 printing dll draw to a .net hdc?

PS. I eventually don't want to have to save the bitmap to
a file. If I can get the correct drawing on a device
context, I'd like to be able to stream that directly to a
byte array and do a Response.Binary write.

If an expert can help with my main issue and my PS. I
would greatly appreciate it.


Nov 20 '05 #2
Thanks for responding.

I dispose of my bitmap and release the dc after I write
the bitmap to a file further down in the code. I know the
vb6 dll works in vb6. The vb6 dll is production software
already working in the hands of hundreds of clients.

I can do a:

gfx.DrawString( "test", New Font("Arial", 24.0F),
Brushes.Black, 0, 0)
gfx.DrawRectang le(New Pen(Color.Black ), New
Rectangle(2, 1, 725, 309))

....inside vb.net and get a bitmap with text and a
rectangle, but when passing the graphic's hdc
(Graphic.GetHdc .ToInt32) I get no drawing whatsoever from
the vb6 dll.
-----Original Message-----
Hi,

You can try calling ReleaseDC after the VB6 dll has finished drawing. Therest of .NET code looks correct. You might also want to debug your VB6 dllto see where it possibly fails.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Metallicraf t" <an*******@disc ussions.microso ft.com> wrote in messagenews:06******* *************** ******@phx.gbl. ..
I have a vb6 application. On the main form is a picture
box with one or two images and several pieces of text
displayed in it. These are created on the fly using gdi32 routines that are all in a referenced, custom dll. I call a PrintImage routine in the dll and pass it only the the
Picturebox.hdc from the main form. The dll's print routine draws to the hdc and it shows up in the picturebox
perfectly.

I would like to do a similar thing with an asp.net page
using my same vb6 custom printing dll. Where Display.aspx has and IMG tag with src="Image.aspx ". Now, in the
Image.aspx.vb code, I make the necessary calls to my dll
to preload some data, then I call the PrintImage routine. What I get back is a black box. Since it's an asp.net page I don't have a picturebox to draw to (even if I did, I'm
not sure the picturebox's Handle would be the same as
vb6's hdc). So I'm trying to create a bitmap in .net where i can eventually get an hdc that I can send to my printing routine in the vb6 dll. Below is the code I use to do this:
Dim bm As Bitmap = New Bitmap(728, 312)
Dim gfx As Graphics = Graphics.FromIm age(bm)
gfx.Clear(Color .White)
Dim hDC As IntPtr = gfx.GetHdc()
prt.hdc = hDC.ToInt32 'prt.hdc is the property in my vb6
dll that receives the hdc to print to

The code runs without error, I save the bitmap to a file, read it into a bytearray, and do a
Response.Binary write...I get a black box (well, now I get a white box since I added the gfx.Clear line above).

Why can't my vb6 printing dll draw to a .net hdc?

PS. I eventually don't want to have to save the bitmap to a file. If I can get the correct drawing on a device
context, I'd like to be able to stream that directly to a byte array and do a Response.Binary write.

If an expert can help with my main issue and my PS. I
would greatly appreciate it.


.

Nov 20 '05 #3
You can try calling a raw GDI function through P/Invoke on the HDC obtained
and see whether it works that way. If yes, you at least know the HDC being
passed is valid.

There also could be problems with VB6 COM DLL running on a separate thread
(as it runs in the Single-Threaded Apartment and ASP .NET is
multi-threaded), and I am not sure HDCs can be passed "as is" between
threads.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Metallicra ft" <an*******@disc ussions.microso ft.com> wrote in message
news:08******** *************** *****@phx.gbl.. .
Thanks for responding.

I dispose of my bitmap and release the dc after I write
the bitmap to a file further down in the code. I know the
vb6 dll works in vb6. The vb6 dll is production software
already working in the hands of hundreds of clients.

I can do a:

gfx.DrawString( "test", New Font("Arial", 24.0F),
Brushes.Black, 0, 0)
gfx.DrawRectang le(New Pen(Color.Black ), New
Rectangle(2, 1, 725, 309))

...inside vb.net and get a bitmap with text and a
rectangle, but when passing the graphic's hdc
(Graphic.GetHdc .ToInt32) I get no drawing whatsoever from
the vb6 dll.
-----Original Message-----
Hi,

You can try calling ReleaseDC after the VB6 dll has

finished drawing. The
rest of .NET code looks correct. You might also want to

debug your VB6 dll
to see where it possibly fails.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Metallicraf t" <an*******@disc ussions.microso ft.com>

wrote in message
news:06******* *************** ******@phx.gbl. ..
I have a vb6 application. On the main form is a picture
box with one or two images and several pieces of text
displayed in it. These are created on the fly using gdi32 routines that are all in a referenced, custom dll. I call a PrintImage routine in the dll and pass it only the the
Picturebox.hdc from the main form. The dll's print routine draws to the hdc and it shows up in the picturebox
perfectly.

I would like to do a similar thing with an asp.net page
using my same vb6 custom printing dll. Where Display.aspx has and IMG tag with src="Image.aspx ". Now, in the
Image.aspx.vb code, I make the necessary calls to my dll
to preload some data, then I call the PrintImage routine. What I get back is a black box. Since it's an asp.net page I don't have a picturebox to draw to (even if I did, I'm
not sure the picturebox's Handle would be the same as
vb6's hdc). So I'm trying to create a bitmap in .net where i can eventually get an hdc that I can send to my printing routine in the vb6 dll. Below is the code I use to do this:
Dim bm As Bitmap = New Bitmap(728, 312)
Dim gfx As Graphics = Graphics.FromIm age(bm)
gfx.Clear(Color .White)
Dim hDC As IntPtr = gfx.GetHdc()
prt.hdc = hDC.ToInt32 'prt.hdc is the property in my vb6
dll that receives the hdc to print to

The code runs without error, I save the bitmap to a file, read it into a bytearray, and do a
Response.Binary write...I get a black box (well, now I get a white box since I added the gfx.Clear line above).

Why can't my vb6 printing dll draw to a .net hdc?

PS. I eventually don't want to have to save the bitmap to a file. If I can get the correct drawing on a device
context, I'd like to be able to stream that directly to a byte array and do a Response.Binary write.

If an expert can help with my main issue and my PS. I
would greatly appreciate it.


.


Nov 20 '05 #4
Well, I've tried a couple different things.

I created a vb6 dll with the following code:

Option Explicit

Private Declare Function TextOut Lib "gdi32"
Alias "TextOutA" (ByVal hdc As Long, ByVal X As Long,
ByVal Y As Long, ByVal lpString As String, ByVal nCount As
Long) As Long

Public Sub DrawText(ByRef hdc As Long)
Dim dl As Long
dl = TextOut(hdc, 0, 0, "TEST", Len("TEST"))
End Sub

....and I compiled it.

I then created a vb application (referencing that dll)
with the following code:

Option Explicit

Private Sub Form_Load()
Picture1.AutoRe draw = True

Dim dt As DrawText.clsDra wText
Set dt = New DrawText.clsDra wText
dt.DrawText (Picture1.hDC)

Picture1.Refres h
End Sub

....the form contains one picture box.

When I run the app, I get the word "TEST" in the picture
box as I would expect.

Next, I created a vb.net Windows app, with only one form,
one button, and a picture box. I referenced my simple dll
above and the only code exists in the button_click:

Dim dt As New DrawText.clsDra wTextClass
dt.DrawText(Pic tureBox1.Handle .ToInt32)
dt = Nothing

PictureBox1.Ref resh()

....I run the app, press the button, and get no text. I've
tried this with and without the Refresh and with and
without the PictureBox (using the form's handle instead)
with the same results.

I have also tried, in a vb.net app, adding this:

<System.Runtime .InteropService s.DllImportAttr ibute
("gdi32.dll" )> _
Private Shared Function TextOut(ByVal hdc As IntPtr,
ByVal X As Integer, ByVal Y As Integer, ByVal lpString As
String, ByVal nCount As Integer) As Integer
End Function

....to the top and in a button_click adding this:

TextOut(Me.Hand le, 100, 100, "TESTING", 7)

....running it, pressing button, and getting no text.

So, even taking my dll and the graphics object out of the
equation, I can't get a gdi32 function, like TextOut, to
write on a .Net device context.

I understand that Object.GetHdc is a pointer, but is
Object.GetHdc.T oInt32 the same as a gdi32 device context
or vb6 picturebox.hdc?
-----Original Message-----
You can try calling a raw GDI function through P/Invoke on the HDC obtainedand see whether it works that way. If yes, you at least know the HDC beingpassed is valid.

There also could be problems with VB6 COM DLL running on a separate thread(as it runs in the Single-Threaded Apartment and ASP .NET ismulti-threaded), and I am not sure HDCs can be passed "as is" betweenthreads.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Metallicraf t" <an*******@disc ussions.microso ft.com> wrote in messagenews:08******* *************** ******@phx.gbl. ..
Thanks for responding.

I dispose of my bitmap and release the dc after I write
the bitmap to a file further down in the code. I know the vb6 dll works in vb6. The vb6 dll is production software already working in the hands of hundreds of clients.

I can do a:

gfx.DrawString( "test", New Font("Arial", 24.0F),
Brushes.Black, 0, 0)
gfx.DrawRectang le(New Pen(Color.Black ), New
Rectangle(2, 1, 725, 309))

...inside vb.net and get a bitmap with text and a
rectangle, but when passing the graphic's hdc
(Graphic.GetHdc .ToInt32) I get no drawing whatsoever from the vb6 dll.
>-----Original Message-----
>Hi,
>
>You can try calling ReleaseDC after the VB6 dll has

finished drawing. The
>rest of .NET code looks correct. You might also want to

debug your VB6 dll
>to see where it possibly fails.
>
>--
>Dmitriy Lapshin [C# / .NET MVP]
>X-Unity Test Studio
>http://x-unity.miik.com.ua/teststudio.aspx
>Bring the power of unit testing to VS .NET IDE
>
>"Metallicraf t" <an*******@disc ussions.microso ft.com>

wrote in message
>news:06******* *************** ******@phx.gbl. ..
>> I have a vb6 application. On the main form is a picture >> box with one or two images and several pieces of text
>> displayed in it. These are created on the fly using

gdi32
>> routines that are all in a referenced, custom dll. I

call
>> a PrintImage routine in the dll and pass it only the the >> Picturebox.hdc from the main form. The dll's print

routine
>> draws to the hdc and it shows up in the picturebox
>> perfectly.
>>
>> I would like to do a similar thing with an asp.net page >> using my same vb6 custom printing dll. Where

Display.aspx
>> has and IMG tag with src="Image.aspx ". Now, in the
>> Image.aspx.vb code, I make the necessary calls to my dll >> to preload some data, then I call the PrintImage

routine.
>> What I get back is a black box. Since it's an asp.net

page
>> I don't have a picturebox to draw to (even if I did, I'm >> not sure the picturebox's Handle would be the same as
>> vb6's hdc). So I'm trying to create a bitmap in .net

where
>> i can eventually get an hdc that I can send to my

printing
>> routine in the vb6 dll. Below is the code I use to do

this:
>>
>> Dim bm As Bitmap = New Bitmap(728, 312)
>> Dim gfx As Graphics = Graphics.FromIm age(bm)
>> gfx.Clear(Color .White)
>> Dim hDC As IntPtr = gfx.GetHdc()
>> prt.hdc = hDC.ToInt32 'prt.hdc is the property in my vb6 >> dll that receives the hdc to print to
>>
>> The code runs without error, I save the bitmap to a

file,
>> read it into a bytearray, and do a
>> Response.Binary write...I get a black box (well, now I

get
>> a white box since I added the gfx.Clear line above).
>>
>> Why can't my vb6 printing dll draw to a .net hdc?
>>
>> PS. I eventually don't want to have to save the
bitmap to
>> a file. If I can get the correct drawing on a device
>> context, I'd like to be able to stream that directly
to a
>> byte array and do a Response.Binary write.
>>
>> If an expert can help with my main issue and my PS. I
>> would greatly appreciate it.
>
>.
>


.

Nov 20 '05 #5
> I understand that Object.GetHdc is a pointer, but is
Object.GetHdc.T oInt32 the same as a gdi32 device context
or vb6 picturebox.hdc?
Yes, they SHOULD be the same according to MSDN. But:
...to the top and in a button_click adding this:

TextOut(Me.Hand le, 100, 100, "TESTING", 7)
What is Me.Handle here? I suppose it is actually HWND and not HDC?
Does your static Main function is marked as [STAThread] by the way?

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Metallicra ft" <an*******@disc ussions.microso ft.com> wrote in message
news:0f******** *************** *****@phx.gbl.. . Well, I've tried a couple different things.

I created a vb6 dll with the following code:

Option Explicit

Private Declare Function TextOut Lib "gdi32"
Alias "TextOutA" (ByVal hdc As Long, ByVal X As Long,
ByVal Y As Long, ByVal lpString As String, ByVal nCount As
Long) As Long

Public Sub DrawText(ByRef hdc As Long)
Dim dl As Long
dl = TextOut(hdc, 0, 0, "TEST", Len("TEST"))
End Sub

...and I compiled it.

I then created a vb application (referencing that dll)
with the following code:

Option Explicit

Private Sub Form_Load()
Picture1.AutoRe draw = True

Dim dt As DrawText.clsDra wText
Set dt = New DrawText.clsDra wText
dt.DrawText (Picture1.hDC)

Picture1.Refres h
End Sub

...the form contains one picture box.

When I run the app, I get the word "TEST" in the picture
box as I would expect.

Next, I created a vb.net Windows app, with only one form,
one button, and a picture box. I referenced my simple dll
above and the only code exists in the button_click:

Dim dt As New DrawText.clsDra wTextClass
dt.DrawText(Pic tureBox1.Handle .ToInt32)
dt = Nothing

PictureBox1.Ref resh()

...I run the app, press the button, and get no text. I've
tried this with and without the Refresh and with and
without the PictureBox (using the form's handle instead)
with the same results.

I have also tried, in a vb.net app, adding this:

<System.Runtime .InteropService s.DllImportAttr ibute
("gdi32.dll" )> _
Private Shared Function TextOut(ByVal hdc As IntPtr,
ByVal X As Integer, ByVal Y As Integer, ByVal lpString As
String, ByVal nCount As Integer) As Integer
End Function

...to the top and in a button_click adding this:

TextOut(Me.Hand le, 100, 100, "TESTING", 7)

...running it, pressing button, and getting no text.

So, even taking my dll and the graphics object out of the
equation, I can't get a gdi32 function, like TextOut, to
write on a .Net device context.

I understand that Object.GetHdc is a pointer, but is
Object.GetHdc.T oInt32 the same as a gdi32 device context
or vb6 picturebox.hdc?
-----Original Message-----
You can try calling a raw GDI function through P/Invoke

on the HDC obtained
and see whether it works that way. If yes, you at least

know the HDC being
passed is valid.

There also could be problems with VB6 COM DLL running on

a separate thread
(as it runs in the Single-Threaded Apartment and ASP .NET

is
multi-threaded), and I am not sure HDCs can be passed "as

is" between
threads.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Metallicraf t" <an*******@disc ussions.microso ft.com>

wrote in message
news:08******* *************** ******@phx.gbl. ..
Thanks for responding.

I dispose of my bitmap and release the dc after I write
the bitmap to a file further down in the code. I know the vb6 dll works in vb6. The vb6 dll is production software already working in the hands of hundreds of clients.

I can do a:

gfx.DrawString( "test", New Font("Arial", 24.0F),
Brushes.Black, 0, 0)
gfx.DrawRectang le(New Pen(Color.Black ), New
Rectangle(2, 1, 725, 309))

...inside vb.net and get a bitmap with text and a
rectangle, but when passing the graphic's hdc
(Graphic.GetHdc .ToInt32) I get no drawing whatsoever from the vb6 dll.

>-----Original Message-----
>Hi,
>
>You can try calling ReleaseDC after the VB6 dll has
finished drawing. The
>rest of .NET code looks correct. You might also want to
debug your VB6 dll
>to see where it possibly fails.
>
>--
>Dmitriy Lapshin [C# / .NET MVP]
>X-Unity Test Studio
>http://x-unity.miik.com.ua/teststudio.aspx
>Bring the power of unit testing to VS .NET IDE
>
>"Metallicraf t" <an*******@disc ussions.microso ft.com>
wrote in message
>news:06******* *************** ******@phx.gbl. ..
>> I have a vb6 application. On the main form is a picture >> box with one or two images and several pieces of text
>> displayed in it. These are created on the fly using
gdi32
>> routines that are all in a referenced, custom dll. I
call
>> a PrintImage routine in the dll and pass it only the the >> Picturebox.hdc from the main form. The dll's print
routine
>> draws to the hdc and it shows up in the picturebox
>> perfectly.
>>
>> I would like to do a similar thing with an asp.net page >> using my same vb6 custom printing dll. Where
Display.aspx
>> has and IMG tag with src="Image.aspx ". Now, in the
>> Image.aspx.vb code, I make the necessary calls to my dll >> to preload some data, then I call the PrintImage
routine.
>> What I get back is a black box. Since it's an asp.net
page
>> I don't have a picturebox to draw to (even if I did, I'm >> not sure the picturebox's Handle would be the same as
>> vb6's hdc). So I'm trying to create a bitmap in .net
where
>> i can eventually get an hdc that I can send to my
printing
>> routine in the vb6 dll. Below is the code I use to do
this:
>>
>> Dim bm As Bitmap = New Bitmap(728, 312)
>> Dim gfx As Graphics = Graphics.FromIm age(bm)
>> gfx.Clear(Color .White)
>> Dim hDC As IntPtr = gfx.GetHdc()
>> prt.hdc = hDC.ToInt32 'prt.hdc is the property in my vb6 >> dll that receives the hdc to print to
>>
>> The code runs without error, I save the bitmap to a
file,
>> read it into a bytearray, and do a
>> Response.Binary write...I get a black box (well, now I
get
>> a white box since I added the gfx.Clear line above).
>>
>> Why can't my vb6 printing dll draw to a .net hdc?
>>
>> PS. I eventually don't want to have to save the bitmap to
>> a file. If I can get the correct drawing on a device
>> context, I'd like to be able to stream that directly to a
>> byte array and do a Response.Binary write.
>>
>> If an expert can help with my main issue and my PS. I
>> would greatly appreciate it.
>
>.
>


.


Nov 20 '05 #6
Me.Handle refers to the form's handle when I tried to just
TextOut to the form instead of a PictureBox. I do believe
it's an hWnd. You'll note in the declaration:

<System.Runtime .InteropService s.DllImportAttr ibute
("gdi32.dll" )> _
Private Shared Function TextOut(ByVal hdc As IntPtr,
ByVal X As Integer, ByVal Y As Integer, ByVal lpString
As String, ByVal nCount As Integer) As Integer
End Function

....hdc is defined as an "IntPtr". I've seen this done in
many examples with APIs that require an hDc that's
normally a Long type, though I don't understand how people
can just change the API declaration like that and have it
still work (I always thought API declarations were very
strict). Nevertheless, I've tried it both ways:

hDc As IntPtr and TextOut(Me.Hand le, etc....

-AND-

hDc As Integer and TextOut(Me.Hand le.ToInt32, etc...

Neither work.

I'm afraid I don't understand your last question
about "[STAThread]".
-----Original Message-----
I understand that Object.GetHdc is a pointer, but is
Object.GetHdc.T oInt32 the same as a gdi32 device context
or vb6 picturebox.hdc?
Yes, they SHOULD be the same according to MSDN. But:
...to the top and in a button_click adding this:

TextOut(Me.Hand le, 100, 100, "TESTING", 7)


What is Me.Handle here? I suppose it is actually HWND and

not HDC?Does your static Main function is marked as [STAThread] by the way?
--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Metallicraf t" <an*******@disc ussions.microso ft.com> wrote in messagenews:0f******* *************** ******@phx.gbl. ..
Well, I've tried a couple different things.

I created a vb6 dll with the following code:

Option Explicit

Private Declare Function TextOut Lib "gdi32"
Alias "TextOutA" (ByVal hdc As Long, ByVal X As Long,
ByVal Y As Long, ByVal lpString As String, ByVal nCount As Long) As Long

Public Sub DrawText(ByRef hdc As Long)
Dim dl As Long
dl = TextOut(hdc, 0, 0, "TEST", Len("TEST"))
End Sub

...and I compiled it.

I then created a vb application (referencing that dll)
with the following code:

Option Explicit

Private Sub Form_Load()
Picture1.AutoRe draw = True

Dim dt As DrawText.clsDra wText
Set dt = New DrawText.clsDra wText
dt.DrawText (Picture1.hDC)

Picture1.Refres h
End Sub

...the form contains one picture box.

When I run the app, I get the word "TEST" in the picture
box as I would expect.

Next, I created a vb.net Windows app, with only one form, one button, and a picture box. I referenced my simple dll above and the only code exists in the button_click:

Dim dt As New DrawText.clsDra wTextClass
dt.DrawText(Pic tureBox1.Handle .ToInt32)
dt = Nothing

PictureBox1.Ref resh()

...I run the app, press the button, and get no text. I've tried this with and without the Refresh and with and
without the PictureBox (using the form's handle instead)
with the same results.

I have also tried, in a vb.net app, adding this:

<System.Runtime .InteropService s.DllImportAttr ibute
("gdi32.dll" )> _
Private Shared Function TextOut(ByVal hdc As IntPtr,
ByVal X As Integer, ByVal Y As Integer, ByVal lpString As String, ByVal nCount As Integer) As Integer
End Function

...to the top and in a button_click adding this:

TextOut(Me.Hand le, 100, 100, "TESTING", 7)

...running it, pressing button, and getting no text.

So, even taking my dll and the graphics object out of the equation, I can't get a gdi32 function, like TextOut, to
write on a .Net device context.

I understand that Object.GetHdc is a pointer, but is
Object.GetHdc.T oInt32 the same as a gdi32 device context
or vb6 picturebox.hdc?
>-----Original Message-----
>You can try calling a raw GDI function through P/Invoke

on the HDC obtained
>and see whether it works that way. If yes, you at least

know the HDC being
>passed is valid.
>
>There also could be problems with VB6 COM DLL running on
a separate thread
>(as it runs in the Single-Threaded Apartment and
ASP .NET is
>multi-threaded), and I am not sure HDCs can be
passed "as is" between
>threads.
>
>--
>Dmitriy Lapshin [C# / .NET MVP]
>X-Unity Test Studio
>http://x-unity.miik.com.ua/teststudio.aspx
>Bring the power of unit testing to VS .NET IDE
>
>"Metallicraf t" <an*******@disc ussions.microso ft.com>

wrote in message
>news:08******* *************** ******@phx.gbl. ..
>> Thanks for responding.
>>
>> I dispose of my bitmap and release the dc after I
write >> the bitmap to a file further down in the code. I know the
>> vb6 dll works in vb6. The vb6 dll is production

software
>> already working in the hands of hundreds of clients.
>>
>> I can do a:
>>
>> gfx.DrawString( "test", New Font("Arial",
24.0F), >> Brushes.Black, 0, 0)
>> gfx.DrawRectang le(New Pen(Color.Black ), New
>> Rectangle(2, 1, 725, 309))
>>
>> ...inside vb.net and get a bitmap with text and a
>> rectangle, but when passing the graphic's hdc
>> (Graphic.GetHdc .ToInt32) I get no drawing whatsoever

from
>> the vb6 dll.
>>
>> >-----Original Message-----
>> >Hi,
>> >
>> >You can try calling ReleaseDC after the VB6 dll has
>> finished drawing. The
>> >rest of .NET code looks correct. You might also want to >> debug your VB6 dll
>> >to see where it possibly fails.
>> >
>> >--
>> >Dmitriy Lapshin [C# / .NET MVP]
>> >X-Unity Test Studio
>> >http://x-unity.miik.com.ua/teststudio.aspx
>> >Bring the power of unit testing to VS .NET IDE
>> >
>> >"Metallicraf t" <an*******@disc ussions.microso ft.com>
>> wrote in message
>> >news:06******* *************** ******@phx.gbl. ..
>> >> I have a vb6 application. On the main form is a

picture
>> >> box with one or two images and several pieces of text >> >> displayed in it. These are created on the fly using >> gdi32
>> >> routines that are all in a referenced, custom dll. I >> call
>> >> a PrintImage routine in the dll and pass it only the the
>> >> Picturebox.hdc from the main form. The dll's print
>> routine
>> >> draws to the hdc and it shows up in the picturebox
>> >> perfectly.
>> >>
>> >> I would like to do a similar thing with an asp.net

page
>> >> using my same vb6 custom printing dll. Where
>> Display.aspx
>> >> has and IMG tag with src="Image.aspx ". Now, in the
>> >> Image.aspx.vb code, I make the necessary calls to
my dll
>> >> to preload some data, then I call the PrintImage
>> routine.
>> >> What I get back is a black box. Since it's an
asp.net >> page
>> >> I don't have a picturebox to draw to (even if I did, I'm
>> >> not sure the picturebox's Handle would be the
same as >> >> vb6's hdc). So I'm trying to create a bitmap in .net >> where
>> >> i can eventually get an hdc that I can send to my
>> printing
>> >> routine in the vb6 dll. Below is the code I use to do >> this:
>> >>
>> >> Dim bm As Bitmap = New Bitmap(728, 312)
>> >> Dim gfx As Graphics = Graphics.FromIm age(bm)
>> >> gfx.Clear(Color .White)
>> >> Dim hDC As IntPtr = gfx.GetHdc()
>> >> prt.hdc = hDC.ToInt32 'prt.hdc is the property in my vb6
>> >> dll that receives the hdc to print to
>> >>
>> >> The code runs without error, I save the bitmap to
a >> file,
>> >> read it into a bytearray, and do a
>> >> Response.Binary write...I get a black box (well, now I >> get
>> >> a white box since I added the gfx.Clear line above). >> >>
>> >> Why can't my vb6 printing dll draw to a .net hdc?
>> >>
>> >> PS. I eventually don't want to have to save the

bitmap
>> to
>> >> a file. If I can get the correct drawing on a device >> >> context, I'd like to be able to stream that directly to
>> a
>> >> byte array and do a Response.Binary write.
>> >>
>> >> If an expert can help with my main issue and my

PS. I >> >> would greatly appreciate it.
>> >
>> >.
>> >
>
>.
>


.

Nov 20 '05 #7
Just to be safe: HDC and HWND are NOT the same and are NOT interchangeable .
And therefore neither of the below will work as it seems you are passing
HWND
where HDC is expected.
hDc As IntPtr and TextOut(Me.Hand le, etc....

-AND-

hDc As Integer and TextOut(Me.Hand le.ToInt32, etc...
From my experience there's no big difference whether you declare an API
function parameter as Int32 or IntPtr (the latter is preferred as you'll
avoid explicit type casts in your code) - both ways work.
I'm afraid I don't understand your last question
about "[STAThread]".
Oops, it's C# syntax. In VB .NET it should look like <STAThread()> or
something like that. It's an attribute that indicated the application is
single-thread and no call to a COM object will be originated by a thread
other that the main one. This is important to the COM subsystem to ensure
instantiated COM objects (and your VB6 DLL is an ActiveX DLL, isn't it?) run
in the expected apartment. This is STA in case of VB6 ActiveX DLLs.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Metallicra ft" <an*******@disc ussions.microso ft.com> wrote in message
news:0a******** *************** *****@phx.gbl.. . Me.Handle refers to the form's handle when I tried to just
TextOut to the form instead of a PictureBox. I do believe
it's an hWnd. You'll note in the declaration:

<System.Runtime .InteropService s.DllImportAttr ibute
("gdi32.dll" )> _
Private Shared Function TextOut(ByVal hdc As IntPtr,
ByVal X As Integer, ByVal Y As Integer, ByVal lpString
As String, ByVal nCount As Integer) As Integer
End Function

...hdc is defined as an "IntPtr". I've seen this done in
many examples with APIs that require an hDc that's
normally a Long type, though I don't understand how people
can just change the API declaration like that and have it
still work (I always thought API declarations were very
strict). Nevertheless, I've tried it both ways:

hDc As IntPtr and TextOut(Me.Hand le, etc....

-AND-

hDc As Integer and TextOut(Me.Hand le.ToInt32, etc...

Neither work.

I'm afraid I don't understand your last question
about "[STAThread]".
-----Original Message-----
I understand that Object.GetHdc is a pointer, but is
Object.GetHdc.T oInt32 the same as a gdi32 device context
or vb6 picturebox.hdc?


Yes, they SHOULD be the same according to MSDN. But:
...to the top and in a button_click adding this:

TextOut(Me.Hand le, 100, 100, "TESTING", 7)


What is Me.Handle here? I suppose it is actually HWND and

not HDC?
Does your static Main function is marked as [STAThread]

by the way?

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Metallicraf t" <an*******@disc ussions.microso ft.com>

wrote in message
news:0f******* *************** ******@phx.gbl. ..
Well, I've tried a couple different things.

I created a vb6 dll with the following code:

Option Explicit

Private Declare Function TextOut Lib "gdi32"
Alias "TextOutA" (ByVal hdc As Long, ByVal X As Long,
ByVal Y As Long, ByVal lpString As String, ByVal nCount As Long) As Long

Public Sub DrawText(ByRef hdc As Long)
Dim dl As Long
dl = TextOut(hdc, 0, 0, "TEST", Len("TEST"))
End Sub

...and I compiled it.

I then created a vb application (referencing that dll)
with the following code:

Option Explicit

Private Sub Form_Load()
Picture1.AutoRe draw = True

Dim dt As DrawText.clsDra wText
Set dt = New DrawText.clsDra wText
dt.DrawText (Picture1.hDC)

Picture1.Refres h
End Sub

...the form contains one picture box.

When I run the app, I get the word "TEST" in the picture
box as I would expect.

Next, I created a vb.net Windows app, with only one form, one button, and a picture box. I referenced my simple dll above and the only code exists in the button_click:

Dim dt As New DrawText.clsDra wTextClass
dt.DrawText(Pic tureBox1.Handle .ToInt32)
dt = Nothing

PictureBox1.Ref resh()

...I run the app, press the button, and get no text. I've tried this with and without the Refresh and with and
without the PictureBox (using the form's handle instead)
with the same results.

I have also tried, in a vb.net app, adding this:

<System.Runtime .InteropService s.DllImportAttr ibute
("gdi32.dll" )> _
Private Shared Function TextOut(ByVal hdc As IntPtr,
ByVal X As Integer, ByVal Y As Integer, ByVal lpString As String, ByVal nCount As Integer) As Integer
End Function

...to the top and in a button_click adding this:

TextOut(Me.Hand le, 100, 100, "TESTING", 7)

...running it, pressing button, and getting no text.

So, even taking my dll and the graphics object out of the equation, I can't get a gdi32 function, like TextOut, to
write on a .Net device context.

I understand that Object.GetHdc is a pointer, but is
Object.GetHdc.T oInt32 the same as a gdi32 device context
or vb6 picturebox.hdc?

>-----Original Message-----
>You can try calling a raw GDI function through P/Invoke
on the HDC obtained
>and see whether it works that way. If yes, you at least
know the HDC being
>passed is valid.
>
>There also could be problems with VB6 COM DLL running on a separate thread
>(as it runs in the Single-Threaded Apartment and ASP .NET is
>multi-threaded), and I am not sure HDCs can be passed "as is" between
>threads.
>
>--
>Dmitriy Lapshin [C# / .NET MVP]
>X-Unity Test Studio
>http://x-unity.miik.com.ua/teststudio.aspx
>Bring the power of unit testing to VS .NET IDE
>
>"Metallicraf t" <an*******@disc ussions.microso ft.com>
wrote in message
>news:08******* *************** ******@phx.gbl. ..
>> Thanks for responding.
>>
>> I dispose of my bitmap and release the dc after I write >> the bitmap to a file further down in the code. I know the
>> vb6 dll works in vb6. The vb6 dll is production
software
>> already working in the hands of hundreds of clients.
>>
>> I can do a:
>>
>> gfx.DrawString( "test", New Font("Arial", 24.0F), >> Brushes.Black, 0, 0)
>> gfx.DrawRectang le(New Pen(Color.Black ), New
>> Rectangle(2, 1, 725, 309))
>>
>> ...inside vb.net and get a bitmap with text and a
>> rectangle, but when passing the graphic's hdc
>> (Graphic.GetHdc .ToInt32) I get no drawing whatsoever
from
>> the vb6 dll.
>>
>> >-----Original Message-----
>> >Hi,
>> >
>> >You can try calling ReleaseDC after the VB6 dll has
>> finished drawing. The
>> >rest of .NET code looks correct. You might also want to >> debug your VB6 dll
>> >to see where it possibly fails.
>> >
>> >--
>> >Dmitriy Lapshin [C# / .NET MVP]
>> >X-Unity Test Studio
>> >http://x-unity.miik.com.ua/teststudio.aspx
>> >Bring the power of unit testing to VS .NET IDE
>> >
>> >"Metallicraf t" <an*******@disc ussions.microso ft.com>
>> wrote in message
>> >news:06******* *************** ******@phx.gbl. ..
>> >> I have a vb6 application. On the main form is a
picture
>> >> box with one or two images and several pieces of text >> >> displayed in it. These are created on the fly using >> gdi32
>> >> routines that are all in a referenced, custom dll. I >> call
>> >> a PrintImage routine in the dll and pass it only the the
>> >> Picturebox.hdc from the main form. The dll's print
>> routine
>> >> draws to the hdc and it shows up in the picturebox
>> >> perfectly.
>> >>
>> >> I would like to do a similar thing with an asp.net
page
>> >> using my same vb6 custom printing dll. Where
>> Display.aspx
>> >> has and IMG tag with src="Image.aspx ". Now, in the
>> >> Image.aspx.vb code, I make the necessary calls to my dll
>> >> to preload some data, then I call the PrintImage
>> routine.
>> >> What I get back is a black box. Since it's an asp.net >> page
>> >> I don't have a picturebox to draw to (even if I did, I'm
>> >> not sure the picturebox's Handle would be the same as >> >> vb6's hdc). So I'm trying to create a bitmap in .net >> where
>> >> i can eventually get an hdc that I can send to my
>> printing
>> >> routine in the vb6 dll. Below is the code I use to do >> this:
>> >>
>> >> Dim bm As Bitmap = New Bitmap(728, 312)
>> >> Dim gfx As Graphics = Graphics.FromIm age(bm)
>> >> gfx.Clear(Color .White)
>> >> Dim hDC As IntPtr = gfx.GetHdc()
>> >> prt.hdc = hDC.ToInt32 'prt.hdc is the property in my vb6
>> >> dll that receives the hdc to print to
>> >>
>> >> The code runs without error, I save the bitmap to a >> file,
>> >> read it into a bytearray, and do a
>> >> Response.Binary write...I get a black box (well, now I >> get
>> >> a white box since I added the gfx.Clear line above). >> >>
>> >> Why can't my vb6 printing dll draw to a .net hdc?
>> >>
>> >> PS. I eventually don't want to have to save the
bitmap
>> to
>> >> a file. If I can get the correct drawing on a device >> >> context, I'd like to be able to stream that directly to
>> a
>> >> byte array and do a Response.Binary write.
>> >>
>> >> If an expert can help with my main issue and my PS. I >> >> would greatly appreciate it.
>> >
>> >.
>> >
>
>.
>


.


Nov 20 '05 #8
I understand what you're saying about the HDC and HWND,
but I thought Handle.ToInt32 was the same as Integer...
where Handle.ToInt32 is the integer representation of the
handle...which is the object's hDc, is this not true?

When I'd pass a Handle to the TextOut API, I have the hDc
in the TextOut declaration defined as an IntPtr. Are
Handle and IntPtr different?

If not, how would one declare the TextOut, for example,
and what would be passed to the hDc parameter?

Maybe that's not important because the REAL dll I'm trying
to get to work cannot be changed. So I need to know if I
can pass a .Net-something into a VB6 Long variable and
have them both be refering to the same hDc.

I understand what you mean about the single-threading, but
I'm still not sure where the "<STAThread ()>" would go or
how it's used. I have very little experience dealing with
threads.
-----Original Message-----
Just to be safe: HDC and HWND are NOT the same and are NOT interchangeable .And therefore neither of the below will work as it seems you are passingHWND
where HDC is expected.
hDc As IntPtr and TextOut(Me.Hand le, etc....

-AND-

hDc As Integer and TextOut(Me.Hand le.ToInt32, etc...
From my experience there's no big difference whether you

declare an APIfunction parameter as Int32 or IntPtr (the latter is preferred as you'llavoid explicit type casts in your code) - both ways work.
I'm afraid I don't understand your last question
about "[STAThread]".
Oops, it's C# syntax. In VB .NET it should look like

<STAThread()> orsomething like that. It's an attribute that indicated the application issingle-thread and no call to a COM object will be originated by a threadother that the main one. This is important to the COM subsystem to ensureinstantiated COM objects (and your VB6 DLL is an ActiveX DLL, isn't it?) runin the expected apartment. This is STA in case of VB6 ActiveX DLLs.
--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Metallicraf t" <an*******@disc ussions.microso ft.com> wrote in messagenews:0a******* *************** ******@phx.gbl. ..
Me.Handle refers to the form's handle when I tried to just TextOut to the form instead of a PictureBox. I do believe it's an hWnd. You'll note in the declaration:

<System.Runtime .InteropService s.DllImportAttr ibute
("gdi32.dll" )> _
Private Shared Function TextOut(ByVal hdc As IntPtr,
ByVal X As Integer, ByVal Y As Integer, ByVal lpString As String, ByVal nCount As Integer) As Integer
End Function

...hdc is defined as an "IntPtr". I've seen this done in many examples with APIs that require an hDc that's
normally a Long type, though I don't understand how people can just change the API declaration like that and have it still work (I always thought API declarations were very
strict). Nevertheless, I've tried it both ways:

hDc As IntPtr and TextOut(Me.Hand le, etc....

-AND-

hDc As Integer and TextOut(Me.Hand le.ToInt32, etc...

Neither work.

I'm afraid I don't understand your last question
about "[STAThread]".
>-----Original Message-----
>> I understand that Object.GetHdc is a pointer, but is
>> Object.GetHdc.T oInt32 the same as a gdi32 device context >> or vb6 picturebox.hdc?
>
>Yes, they SHOULD be the same according to MSDN. But:
>
>> ...to the top and in a button_click adding this:
>>
>> TextOut(Me.Hand le, 100, 100, "TESTING", 7)
>
>What is Me.Handle here? I suppose it is actually HWND and
not HDC?
>Does your static Main function is marked as [STAThread]

by the way?
>
>--
>Dmitriy Lapshin [C# / .NET MVP]
>X-Unity Test Studio
>http://x-unity.miik.com.ua/teststudio.aspx
>Bring the power of unit testing to VS .NET IDE
>
>"Metallicraf t" <an*******@disc ussions.microso ft.com>

wrote in message
>news:0f******* *************** ******@phx.gbl. ..
>> Well, I've tried a couple different things.
>>
>> I created a vb6 dll with the following code:
>>
>> Option Explicit
>>
>> Private Declare Function TextOut Lib "gdi32"
>> Alias "TextOutA" (ByVal hdc As Long, ByVal X As Long,
>> ByVal Y As Long, ByVal lpString As String, ByVal
nCount As
>> Long) As Long
>>
>> Public Sub DrawText(ByRef hdc As Long)
>> Dim dl As Long
>> dl = TextOut(hdc, 0, 0, "TEST", Len("TEST"))
>> End Sub
>>
>> ...and I compiled it.
>>
>> I then created a vb application (referencing that
dll) >> with the following code:
>>
>> Option Explicit
>>
>> Private Sub Form_Load()
>> Picture1.AutoRe draw = True
>>
>> Dim dt As DrawText.clsDra wText
>> Set dt = New DrawText.clsDra wText
>> dt.DrawText (Picture1.hDC)
>>
>> Picture1.Refres h
>> End Sub
>>
>> ...the form contains one picture box.
>>
>> When I run the app, I get the word "TEST" in the picture >> box as I would expect.
>>
>> Next, I created a vb.net Windows app, with only one

form,
>> one button, and a picture box. I referenced my simple dll
>> above and the only code exists in the button_click:
>>
>> Dim dt As New DrawText.clsDra wTextClass
>> dt.DrawText(Pic tureBox1.Handle .ToInt32)
>> dt = Nothing
>>
>> PictureBox1.Ref resh()
>>
>> ...I run the app, press the button, and get no text.

I've
>> tried this with and without the Refresh and with and
>> without the PictureBox (using the form's handle
instead) >> with the same results.
>>
>> I have also tried, in a vb.net app, adding this:
>>
>> <System.Runtime .InteropService s.DllImportAttr ibute >> ("gdi32.dll" )> _
>> Private Shared Function TextOut(ByVal hdc As IntPtr, >> ByVal X As Integer, ByVal Y As Integer, ByVal lpString As
>> String, ByVal nCount As Integer) As Integer
>> End Function
>>
>> ...to the top and in a button_click adding this:
>>
>> TextOut(Me.Hand le, 100, 100, "TESTING", 7)
>>
>> ...running it, pressing button, and getting no text.
>>
>> So, even taking my dll and the graphics object out of

the
>> equation, I can't get a gdi32 function, like
TextOut, to >> write on a .Net device context.
>>
>> I understand that Object.GetHdc is a pointer, but is
>> Object.GetHdc.T oInt32 the same as a gdi32 device context >> or vb6 picturebox.hdc?
>>
>> >-----Original Message-----
>> >You can try calling a raw GDI function through P/Invoke >> on the HDC obtained
>> >and see whether it works that way. If yes, you at least >> know the HDC being
>> >passed is valid.
>> >
>> >There also could be problems with VB6 COM DLL running on
>> a separate thread
>> >(as it runs in the Single-Threaded Apartment and

ASP .NET
>> is
>> >multi-threaded), and I am not sure HDCs can be

passed "as
>> is" between
>> >threads.
>> >
>> >--
>> >Dmitriy Lapshin [C# / .NET MVP]
>> >X-Unity Test Studio
>> >http://x-unity.miik.com.ua/teststudio.aspx
>> >Bring the power of unit testing to VS .NET IDE
>> >
>> >"Metallicraf t" <an*******@disc ussions.microso ft.com>
>> wrote in message
>> >news:08******* *************** ******@phx.gbl. ..
>> >> Thanks for responding.
>> >>
>> >> I dispose of my bitmap and release the dc after I

write
>> >> the bitmap to a file further down in the code. I

know
>> the
>> >> vb6 dll works in vb6. The vb6 dll is production
>> software
>> >> already working in the hands of hundreds of
clients. >> >>
>> >> I can do a:
>> >>
>> >> gfx.DrawString( "test", New Font("Arial",

24.0F),
>> >> Brushes.Black, 0, 0)
>> >> gfx.DrawRectang le(New Pen(Color.Black ), New >> >> Rectangle(2, 1, 725, 309))
>> >>
>> >> ...inside vb.net and get a bitmap with text and a
>> >> rectangle, but when passing the graphic's hdc
>> >> (Graphic.GetHdc .ToInt32) I get no drawing whatsoever >> from
>> >> the vb6 dll.
>> >>
>> >> >-----Original Message-----
>> >> >Hi,
>> >> >
>> >> >You can try calling ReleaseDC after the VB6 dll has >> >> finished drawing. The
>> >> >rest of .NET code looks correct. You might also

want to
>> >> debug your VB6 dll
>> >> >to see where it possibly fails.
>> >> >
>> >> >--
>> >> >Dmitriy Lapshin [C# / .NET MVP]
>> >> >X-Unity Test Studio
>> >> >http://x-unity.miik.com.ua/teststudio.aspx
>> >> >Bring the power of unit testing to VS .NET IDE
>> >> >
>> >> >"Metallicraf t" <an*******@disc ussions.microso ft.com> >> >> wrote in message
>> >> >news:06******* *************** ******@phx.gbl. ..
>> >> >> I have a vb6 application. On the main form is a
>> picture
>> >> >> box with one or two images and several pieces of text
>> >> >> displayed in it. These are created on the fly

using
>> >> gdi32
>> >> >> routines that are all in a referenced, custom

dll. I
>> >> call
>> >> >> a PrintImage routine in the dll and pass it
only the
>> the
>> >> >> Picturebox.hdc from the main form. The dll's
print >> >> routine
>> >> >> draws to the hdc and it shows up in the picturebox >> >> >> perfectly.
>> >> >>
>> >> >> I would like to do a similar thing with an asp.net >> page
>> >> >> using my same vb6 custom printing dll. Where
>> >> Display.aspx
>> >> >> has and IMG tag with src="Image.aspx ". Now, in the >> >> >> Image.aspx.vb code, I make the necessary calls to my
>> dll
>> >> >> to preload some data, then I call the
PrintImage >> >> routine.
>> >> >> What I get back is a black box. Since it's an

asp.net
>> >> page
>> >> >> I don't have a picturebox to draw to (even if I

did,
>> I'm
>> >> >> not sure the picturebox's Handle would be the

same as
>> >> >> vb6's hdc). So I'm trying to create a bitmap

in .net
>> >> where
>> >> >> i can eventually get an hdc that I can send to my >> >> printing
>> >> >> routine in the vb6 dll. Below is the code I use

to do
>> >> this:
>> >> >>
>> >> >> Dim bm As Bitmap = New Bitmap(728, 312)
>> >> >> Dim gfx As Graphics = Graphics.FromIm age(bm)
>> >> >> gfx.Clear(Color .White)
>> >> >> Dim hDC As IntPtr = gfx.GetHdc()
>> >> >> prt.hdc = hDC.ToInt32 'prt.hdc is the property in my
>> vb6
>> >> >> dll that receives the hdc to print to
>> >> >>
>> >> >> The code runs without error, I save the bitmap
to a
>> >> file,
>> >> >> read it into a bytearray, and do a
>> >> >> Response.Binary write...I get a black box (well,

now I
>> >> get
>> >> >> a white box since I added the gfx.Clear line

above).
>> >> >>
>> >> >> Why can't my vb6 printing dll draw to a .net

hdc? >> >> >>
>> >> >> PS. I eventually don't want to have to save the
>> bitmap
>> >> to
>> >> >> a file. If I can get the correct drawing on a

device
>> >> >> context, I'd like to be able to stream that

directly
>> to
>> >> a
>> >> >> byte array and do a Response.Binary write.
>> >> >>
>> >> >> If an expert can help with my main issue and my

PS. I
>> >> >> would greatly appreciate it.
>> >> >
>> >> >.
>> >> >
>> >
>> >.
>> >
>
>.
>


.

Nov 20 '05 #9
> where Handle.ToInt32 is the integer representation of the
handle...which is the object's hDc, is this not true?
Handle.ToInt32 _IS_ the integer representation of a handle, but Form.Handle
is a window handle (HWND),
not a GDI device context handle (HDC). To obtain an HDC for a form or a
control, you should employ
the Graphics class and contruct it by using the static FromHwnd method:

Dim g AsGraphics = Graphics.FromHw nd(Me.Handle)

Dim hdc As IntPtr = g.GetHdc()
TextOut(hdc, 100, 100, "TESTING", 7)
g.ReleaseHdc(hd c)

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Metallicra ft" <an*******@disc ussions.microso ft.com> wrote in message
news:0c******** *************** *****@phx.gbl.. . I understand what you're saying about the HDC and HWND,
but I thought Handle.ToInt32 was the same as Integer...
where Handle.ToInt32 is the integer representation of the
handle...which is the object's hDc, is this not true?

When I'd pass a Handle to the TextOut API, I have the hDc
in the TextOut declaration defined as an IntPtr. Are
Handle and IntPtr different?

If not, how would one declare the TextOut, for example,
and what would be passed to the hDc parameter?

Maybe that's not important because the REAL dll I'm trying
to get to work cannot be changed. So I need to know if I
can pass a .Net-something into a VB6 Long variable and
have them both be refering to the same hDc.

I understand what you mean about the single-threading, but
I'm still not sure where the "<STAThread ()>" would go or
how it's used. I have very little experience dealing with
threads.
-----Original Message-----
Just to be safe: HDC and HWND are NOT the same and are

NOT interchangeable .
And therefore neither of the below will work as it seems

you are passing
HWND
where HDC is expected.
hDc As IntPtr and TextOut(Me.Hand le, etc....

-AND-

hDc As Integer and TextOut(Me.Hand le.ToInt32, etc...


From my experience there's no big difference whether you

declare an API
function parameter as Int32 or IntPtr (the latter is

preferred as you'll
avoid explicit type casts in your code) - both ways work.
I'm afraid I don't understand your last question
about "[STAThread]".


Oops, it's C# syntax. In VB .NET it should look like

<STAThread()> or
something like that. It's an attribute that indicated the

application is
single-thread and no call to a COM object will be

originated by a thread
other that the main one. This is important to the COM

subsystem to ensure
instantiated COM objects (and your VB6 DLL is an ActiveX

DLL, isn't it?) run
in the expected apartment. This is STA in case of VB6

ActiveX DLLs.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Metallicraf t" <an*******@disc ussions.microso ft.com>

wrote in message
news:0a******* *************** ******@phx.gbl. ..
Me.Handle refers to the form's handle when I tried to just TextOut to the form instead of a PictureBox. I do believe it's an hWnd. You'll note in the declaration:

<System.Runtime .InteropService s.DllImportAttr ibute
("gdi32.dll" )> _
Private Shared Function TextOut(ByVal hdc As IntPtr,
ByVal X As Integer, ByVal Y As Integer, ByVal lpString As String, ByVal nCount As Integer) As Integer
End Function

...hdc is defined as an "IntPtr". I've seen this done in many examples with APIs that require an hDc that's
normally a Long type, though I don't understand how people can just change the API declaration like that and have it still work (I always thought API declarations were very
strict). Nevertheless, I've tried it both ways:

hDc As IntPtr and TextOut(Me.Hand le, etc....

-AND-

hDc As Integer and TextOut(Me.Hand le.ToInt32, etc...

Neither work.

I'm afraid I don't understand your last question
about "[STAThread]".

>-----Original Message-----
>> I understand that Object.GetHdc is a pointer, but is
>> Object.GetHdc.T oInt32 the same as a gdi32 device context >> or vb6 picturebox.hdc?
>
>Yes, they SHOULD be the same according to MSDN. But:
>
>> ...to the top and in a button_click adding this:
>>
>> TextOut(Me.Hand le, 100, 100, "TESTING", 7)
>
>What is Me.Handle here? I suppose it is actually HWND and not HDC?
>Does your static Main function is marked as [STAThread]
by the way?
>
>--
>Dmitriy Lapshin [C# / .NET MVP]
>X-Unity Test Studio
>http://x-unity.miik.com.ua/teststudio.aspx
>Bring the power of unit testing to VS .NET IDE
>
>"Metallicraf t" <an*******@disc ussions.microso ft.com>
wrote in message
>news:0f******* *************** ******@phx.gbl. ..
>> Well, I've tried a couple different things.
>>
>> I created a vb6 dll with the following code:
>>
>> Option Explicit
>>
>> Private Declare Function TextOut Lib "gdi32"
>> Alias "TextOutA" (ByVal hdc As Long, ByVal X As Long,
>> ByVal Y As Long, ByVal lpString As String, ByVal nCount As
>> Long) As Long
>>
>> Public Sub DrawText(ByRef hdc As Long)
>> Dim dl As Long
>> dl = TextOut(hdc, 0, 0, "TEST", Len("TEST"))
>> End Sub
>>
>> ...and I compiled it.
>>
>> I then created a vb application (referencing that dll) >> with the following code:
>>
>> Option Explicit
>>
>> Private Sub Form_Load()
>> Picture1.AutoRe draw = True
>>
>> Dim dt As DrawText.clsDra wText
>> Set dt = New DrawText.clsDra wText
>> dt.DrawText (Picture1.hDC)
>>
>> Picture1.Refres h
>> End Sub
>>
>> ...the form contains one picture box.
>>
>> When I run the app, I get the word "TEST" in the picture >> box as I would expect.
>>
>> Next, I created a vb.net Windows app, with only one
form,
>> one button, and a picture box. I referenced my simple dll
>> above and the only code exists in the button_click:
>>
>> Dim dt As New DrawText.clsDra wTextClass
>> dt.DrawText(Pic tureBox1.Handle .ToInt32)
>> dt = Nothing
>>
>> PictureBox1.Ref resh()
>>
>> ...I run the app, press the button, and get no text.
I've
>> tried this with and without the Refresh and with and
>> without the PictureBox (using the form's handle instead) >> with the same results.
>>
>> I have also tried, in a vb.net app, adding this:
>>
>> <System.Runtime .InteropService s.DllImportAttr ibute >> ("gdi32.dll" )> _
>> Private Shared Function TextOut(ByVal hdc As IntPtr, >> ByVal X As Integer, ByVal Y As Integer, ByVal lpString As
>> String, ByVal nCount As Integer) As Integer
>> End Function
>>
>> ...to the top and in a button_click adding this:
>>
>> TextOut(Me.Hand le, 100, 100, "TESTING", 7)
>>
>> ...running it, pressing button, and getting no text.
>>
>> So, even taking my dll and the graphics object out of
the
>> equation, I can't get a gdi32 function, like TextOut, to >> write on a .Net device context.
>>
>> I understand that Object.GetHdc is a pointer, but is
>> Object.GetHdc.T oInt32 the same as a gdi32 device context >> or vb6 picturebox.hdc?
>>
>> >-----Original Message-----
>> >You can try calling a raw GDI function through P/Invoke >> on the HDC obtained
>> >and see whether it works that way. If yes, you at least >> know the HDC being
>> >passed is valid.
>> >
>> >There also could be problems with VB6 COM DLL running on
>> a separate thread
>> >(as it runs in the Single-Threaded Apartment and
ASP .NET
>> is
>> >multi-threaded), and I am not sure HDCs can be
passed "as
>> is" between
>> >threads.
>> >
>> >--
>> >Dmitriy Lapshin [C# / .NET MVP]
>> >X-Unity Test Studio
>> >http://x-unity.miik.com.ua/teststudio.aspx
>> >Bring the power of unit testing to VS .NET IDE
>> >
>> >"Metallicraf t" <an*******@disc ussions.microso ft.com>
>> wrote in message
>> >news:08******* *************** ******@phx.gbl. ..
>> >> Thanks for responding.
>> >>
>> >> I dispose of my bitmap and release the dc after I
write
>> >> the bitmap to a file further down in the code. I
know
>> the
>> >> vb6 dll works in vb6. The vb6 dll is production
>> software
>> >> already working in the hands of hundreds of clients. >> >>
>> >> I can do a:
>> >>
>> >> gfx.DrawString( "test", New Font("Arial",
24.0F),
>> >> Brushes.Black, 0, 0)
>> >> gfx.DrawRectang le(New Pen(Color.Black ), New >> >> Rectangle(2, 1, 725, 309))
>> >>
>> >> ...inside vb.net and get a bitmap with text and a
>> >> rectangle, but when passing the graphic's hdc
>> >> (Graphic.GetHdc .ToInt32) I get no drawing whatsoever >> from
>> >> the vb6 dll.
>> >>
>> >> >-----Original Message-----
>> >> >Hi,
>> >> >
>> >> >You can try calling ReleaseDC after the VB6 dll has >> >> finished drawing. The
>> >> >rest of .NET code looks correct. You might also
want to
>> >> debug your VB6 dll
>> >> >to see where it possibly fails.
>> >> >
>> >> >--
>> >> >Dmitriy Lapshin [C# / .NET MVP]
>> >> >X-Unity Test Studio
>> >> >http://x-unity.miik.com.ua/teststudio.aspx
>> >> >Bring the power of unit testing to VS .NET IDE
>> >> >
>> >> >"Metallicraf t" <an*******@disc ussions.microso ft.com> >> >> wrote in message
>> >> >news:06******* *************** ******@phx.gbl. ..
>> >> >> I have a vb6 application. On the main form is a
>> picture
>> >> >> box with one or two images and several pieces of text
>> >> >> displayed in it. These are created on the fly
using
>> >> gdi32
>> >> >> routines that are all in a referenced, custom
dll. I
>> >> call
>> >> >> a PrintImage routine in the dll and pass it only the
>> the
>> >> >> Picturebox.hdc from the main form. The dll's print >> >> routine
>> >> >> draws to the hdc and it shows up in the picturebox >> >> >> perfectly.
>> >> >>
>> >> >> I would like to do a similar thing with an asp.net >> page
>> >> >> using my same vb6 custom printing dll. Where
>> >> Display.aspx
>> >> >> has and IMG tag with src="Image.aspx ". Now, in the >> >> >> Image.aspx.vb code, I make the necessary calls to my
>> dll
>> >> >> to preload some data, then I call the PrintImage >> >> routine.
>> >> >> What I get back is a black box. Since it's an
asp.net
>> >> page
>> >> >> I don't have a picturebox to draw to (even if I
did,
>> I'm
>> >> >> not sure the picturebox's Handle would be the
same as
>> >> >> vb6's hdc). So I'm trying to create a bitmap
in .net
>> >> where
>> >> >> i can eventually get an hdc that I can send to my >> >> printing
>> >> >> routine in the vb6 dll. Below is the code I use
to do
>> >> this:
>> >> >>
>> >> >> Dim bm As Bitmap = New Bitmap(728, 312)
>> >> >> Dim gfx As Graphics = Graphics.FromIm age(bm)
>> >> >> gfx.Clear(Color .White)
>> >> >> Dim hDC As IntPtr = gfx.GetHdc()
>> >> >> prt.hdc = hDC.ToInt32 'prt.hdc is the property in my
>> vb6
>> >> >> dll that receives the hdc to print to
>> >> >>
>> >> >> The code runs without error, I save the bitmap to a
>> >> file,
>> >> >> read it into a bytearray, and do a
>> >> >> Response.Binary write...I get a black box (well,
now I
>> >> get
>> >> >> a white box since I added the gfx.Clear line
above).
>> >> >>
>> >> >> Why can't my vb6 printing dll draw to a .net hdc? >> >> >>
>> >> >> PS. I eventually don't want to have to save the
>> bitmap
>> >> to
>> >> >> a file. If I can get the correct drawing on a
device
>> >> >> context, I'd like to be able to stream that
directly
>> to
>> >> a
>> >> >> byte array and do a Response.Binary write.
>> >> >>
>> >> >> If an expert can help with my main issue and my
PS. I
>> >> >> would greatly appreciate it.
>> >> >
>> >> >.
>> >> >
>> >
>> >.
>> >
>
>.
>


.


Nov 20 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
5615
by: matt | last post by:
I am trying to draw a rectangle in a dialog window. I put the code in OnPaint(), even though I see a message put there by VC++ saying "Do not call CDialog::OnPaint() for painting messages." I don't know what that means. The code below worked in one dialog window, but not in another. Any idea what could cause this to work in one window...
1
3057
by: Abd | last post by:
Hiiiiiii I am programming in Visual C++ 6.0 I want to get RGB percent of all of pixls of screen, and so I need to get Device Context of entire screen. I know that CClientDC dc(this) gets a divice context handler for the active window of my application that is running. I want to know what expression must be the argument of constructor of...
0
1204
by: sachin | last post by:
Please read the code snippets belo snippet 1: Drawing rectangle using Device Context ----------------------------------------------------------------[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll") private static extern bool Rectangle IntPtr hdc int ulCornerX, int ulCornerY int lrCornerX, int lrCornerY) public void...
4
4513
by: Peter Oliphant | last post by:
There doesn't seem to be any documentation on how to create and/or use an instance of System::Drawing::Graphics. On-line MSDN talks about this class, and says: " The Graphics class provides methods for drawing objects to the display device. A Graphics object is associated with a specific device context. " Great. So how about explaining...
9
3163
by: davetelling | last post by:
I am not a programmer, I'm an engineer trying to make an interface to a product I'm designing. I have used C# to make a form that interrogates the unit via the serial port and receives the data. I want to be able to draw lines in a picturebox based upon certain data points I have received. I dragged a picturebox from the toolbar onto my form,...
8
4299
by: Joergen Bech | last post by:
Suppose I have Dim bm As New Bitmap(16, 16,Imaging.PixelFormat.Format8bppIndexed) I cannot use Dim g As Graphics = Graphics.FromImage(bmdest) Dim hdc As IntPtr = g.GetHdc() as the FromImage call will fail for indexed bitmaps.
3
2954
by: gudguy | last post by:
dear gurus, I have a 3rd party component that triggers an event and exposed the Width and Height of the video frame, plus the Device context handle (hDC) for the data packet (it is supposed to be a bitmap packets). I want to know how to save this to System::Drawing::Bitmap object i.e. System::Drawing::Bitmap SaveToBitmap(int width, int...
6
5376
by: =?Utf-8?B?bmNvbG9zaQ==?= | last post by:
Is it possible to render a winform directly to a memory device context instead of rendering to a display device context ?
2
3263
by: ouzsharp | last post by:
The follwing class works well when used in a C# console application: namespace ScreenShotDemo { /// <summary> /// Provides functions to capture the entire screen, or a particular window, and save it to a file. /// </summary> public class ScreenCapture {
0
7805
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8052
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7815
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8098
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5300
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3740
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3749
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2243
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1060
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.