Groups | Blog | Home
all groups > dotnet drawing api > december 2003 >

dotnet drawing api : Copy a form's Paint Event


james
12/28/2003 8:12:13 PM
I hope I can explain this right. I found some VB.NET code (converted from a
C# program) that uses Windows Image Aquisition to scan images from an
attached scanner. It only works in Windows 2000 and XP.
The code copies the bits from the scanner to an array and then overrides the
form's paint even and paints the form with the data from the array. If I try
to copy the Background image of the form to a Bitmap or an Image, I get a
blank image. Is there some way to paint on a Bitmap or Image ? Here is a
bit of the code that does the painting:

Protected Overrides Sub OnPaint(ByVal e As
System.Windows.Forms.PaintEventArgs)

Dim cltrect As Rectangle = ClientRectangle

Dim clprect As Rectangle = e.ClipRectangle

Dim scrol As Point = AutoScrollPosition

Dim realrect As Rectangle = clprect

realrect.X -= scrol.X

realrect.Y -= scrol.Y

Dim brbg As SolidBrush = New SolidBrush(Color.Black)

If (realrect.Right > bmprect.Width) Then

Dim bgri As Rectangle = clprect

Dim ovri As Integer = bmprect.Width - realrect.X

If (ovri > 0) Then

bgri.X += ovri

bgri.Width -= ovri

End If

e.Graphics.FillRectangle(brbg, bgri)

End If

If (realrect.Bottom > bmprect.Height) Then

Dim bgbo As Rectangle = clprect

Dim ovbo As Integer = bmprect.Height - realrect.Y

If (ovbo > 0) Then

bgbo.Y += ovbo

bgbo.Height -= ovbo

End If

e.Graphics.FillRectangle(brbg, bgbo)

End If

realrect.Intersect(bmprect)

If Not (realrect.IsEmpty) Then

Dim bot As Integer = bmprect.Height - realrect.Bottom

Dim hdc As IntPtr = e.Graphics.GetHdc()

SetDIBitsToDevice(hdc, clprect.X, clprect.Y, realrect.Width,
realrect.Height, realrect.X, bot, 0, bmprect.Height, pixptr, bmpptr, 0)

e.Graphics.ReleaseHdc(hdc)

End If

End Sub



I have a Module called PassImage with a Global object called: scanImg (
like this: Public scanImg as Image)

that I can access from any of my other forms in my program. I can scan &
save the painted image just fine. But, I would like to be able to copy the
scanned image back to my main form's picturebox, where I can edit it.

Any ideas?

james



james
12/29/2003 12:26:19 PM
Thanks for the response James. The app. I am working with currently
over-rides the paint event of a form and draws(paints) the scanned image to
the form(this is a Child form of an MDI Parent form). The scanned image is
stored in an array. My problem is I want to copy the array image information
to a Bitmap (or Image object) and then copy that to my main form's
Picturebox control, so my user can edit the resulting image. And once
editing(or converting or whatever) takes place, my user can then save the
results. I had at first thought the image was actually being painted on as
the Background Image of the Child Form, but, if I copy the background image
of the Child form, it comes up with the regular background color and not the
painted image.
This is a VB.NET application. I know the answer is probably simple and
staring me right in the face.
I have tried the Graphics,FromImage(Bitmap) as you suggested, but, since
there is no Bitmap being created in the first place to copy, the function
fails. And I have also used the CreateGraphics function too. Same results.
Either I am not trapping the contents of the Child Form's paint event in the
correct place or I am completely mis-understanding how the original code is
working. (which may be the case too).
If you are interested, (or if anyone is interested) I would be glad to send
you a zipped copy of the original
VB.NET sample app. (with code) to see what the problem is.
Scanning and Saving the image is easy. Then just going back to the main
app. (form) and re-loading the saved image works fine. But, I feel it is an
unneccessary step in my application. The original code was a
Twain WIA sample from : http://www.codeproject.com and was written in C#.
Another person copied and converted it to VB.NET. But, the second person,
was/is unfamiliar with Twain to the point that he could not answer any
questions.
Any suggestions would be appreciated.
(sorry for the long post)
james


[quoted text, click to view]

James Westgate (Crainiate)
12/29/2003 1:49:17 PM
Hi James,

What you want to do is create a new Bitmap and then create a Graphics object
from the bitmap using the static method graphics.FromImage(Bitmap)

Draw the scanned image using this graphics object and then paint to the
form, picture box etc using the bitmap.

James


--
Create interactive flowcharts, diagrams and UML models with ERM3 at
http://www.crainiate.net



[quoted text, click to view]

AddThis Social Bookmark Button