473,406 Members | 2,378 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

A paint event question

The following code will display a tif file on a form. When another form is
moved over the tif image, the tif image is erased where the form was moved.
A paint event occurs when this happens. My question is..how would I save the
tif images as soon as it is initially painted, and then restore the image in
a paint event. It seems to me that there should be some sort of a SaveImage
method and a RestoreImage method that would be fast. Note: Page1 is the form
name, and FileName is passed from a routine on another form.

Public Filename as string
dim gr as graphics

Public Sub DrawTif()
Dim bm As Bitmap

bm = Bitmap.FromFile(FileName)

gr = Me.CreateGraphics
gr.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
gr.DrawImage(bm, New Rectangle(0, 0, 400, 500))
can I save the image here End Sub

sub Page1_paint> and restore it here

end sub

sub Page1_Closing
gr.dispose
end sub
Nov 21 '05 #1
7 1811
Hi,

"hamil" <ha***@discussions.microsoft.com> wrote in message
news:ED**********************************@microsof t.com...
The following code will display a tif file on a form. When another form is
moved over the tif image, the tif image is erased where the form was
moved.
A paint event occurs when this happens. My question is..how would I save
the
tif images as soon as it is initially painted, and then restore the image
in
a paint event. It seems to me that there should be some sort of a
SaveImage
But why would you want to save and restore it, it is already in a Bitmap,
just (re)draw it inside OnPaint. Also, never store a Graphics object
obtained with CreateGraphics, you must always Dispose it before the
eventhandler finishes.

See code below.
method and a RestoreImage method that would be fast. Note: Page1 is the
form
name, and FileName is passed from a routine on another form.


Public Filename as String
Dim bm As Bitmap

Public Sub DrawTif()
bm = Bitmap.FromFile(FileName)
End Sub

Sub Page1_paint
If ( Not bm Is Nothing ) Then
e.Graphics.InterpolationMode =
Drawing2D.InterpolationMode.HighQualityBicubic
e.DrawImage(bm, New Rectangle(0, 0, 400, 500))
End If
End sub

Sub Page1_Closing
bm.Dispose
bm = Nothing
End Sub

HTH,
Greetings
Nov 21 '05 #2


"Bart Mermuys" wrote:
Hi,

"hamil" <ha***@discussions.microsoft.com> wrote in message
news:ED**********************************@microsof t.com...
The following code will display a tif file on a form. When another form is
moved over the tif image, the tif image is erased where the form was
moved.
A paint event occurs when this happens. My question is..how would I save
the
tif images as soon as it is initially painted, and then restore the image
in
a paint event. It seems to me that there should be some sort of a
SaveImage


But why would you want to save and restore it, it is already in a Bitmap,
just (re)draw it inside OnPaint. Also, never store a Graphics object
obtained with CreateGraphics, you must always Dispose it before the
eventhandler finishes.

See code below.
method and a RestoreImage method that would be fast. Note: Page1 is the
form
name, and FileName is passed from a routine on another form.


Public Filename as String
Dim bm As Bitmap

Public Sub DrawTif()
bm = Bitmap.FromFile(FileName)
End Sub

Sub Page1_paint
If ( Not bm Is Nothing ) Then
e.Graphics.InterpolationMode =
Drawing2D.InterpolationMode.HighQualityBicubic
e.DrawImage(bm, New Rectangle(0, 0, 400, 500))
End If
End sub

Sub Page1_Closing
bm.Dispose
bm = Nothing
End Sub

HTH,
Greetings

Bert..

Your code works, but the paint event is slow. My image is a tif that was
originally an 8-1/2 X 11 page, black-white sheet scanned at 300 DPI, that
results in about 8,200,000 pixels. I use a Group four Fax compression that
makes the file about 50K bytes. The code " bm = bitmap.from file..." will
decompress and regenerate the 8,200,000 bit image. Since I am now displaying
the image on a form on by screen at about 1100 X 850 pixels, (935,000 bits),
interpolation must be used along with the scaling to get a good image. I am
pretty sure the scaling and interpolation is what is taking up most of the
time as this is done at every paint event.

It seems to me that there should be some way to scale, interpolate, and draw
the image to a buffer that is saved. Then at the paint event, the buffer
could be read and drawn without having to go through the scaling and
interpolation every time.

Any thoughts here? Maybe this cannot be done in VB.

Hamil.

Nov 21 '05 #3
Hi,

"hamil" <ha***@discussions.microsoft.com> wrote in message
news:D4**********************************@microsof t.com...


"Bart Mermuys" wrote:
Hi,

"hamil" <ha***@discussions.microsoft.com> wrote in message
news:ED**********************************@microsof t.com...
> The following code will display a tif file on a form. When another form
> is
> moved over the tif image, the tif image is erased where the form was
> moved.
> A paint event occurs when this happens. My question is..how would I
> save
> the
> tif images as soon as it is initially painted, and then restore the
> image
> in
> a paint event. It seems to me that there should be some sort of a
> SaveImage
But why would you want to save and restore it, it is already in a Bitmap,
just (re)draw it inside OnPaint. Also, never store a Graphics object
obtained with CreateGraphics, you must always Dispose it before the
eventhandler finishes.

See code below.
> method and a RestoreImage method that would be fast. Note: Page1 is the
> form
> name, and FileName is passed from a routine on another form.


Public Filename as String
Dim bm As Bitmap

Public Sub DrawTif()
bm = Bitmap.FromFile(FileName)
End Sub

Sub Page1_paint
If ( Not bm Is Nothing ) Then
e.Graphics.InterpolationMode =
Drawing2D.InterpolationMode.HighQualityBicubic
e.DrawImage(bm, New Rectangle(0, 0, 400, 500))
End If
End sub

Sub Page1_Closing
bm.Dispose
bm = Nothing
End Sub

HTH,
Greetings

Bert..

Your code works, but the paint event is slow. My image is a tif that was
originally an 8-1/2 X 11 page, black-white sheet scanned at 300 DPI, that
results in about 8,200,000 pixels. I use a Group four Fax compression that
makes the file about 50K bytes. The code " bm = bitmap.from file..." will
decompress and regenerate the 8,200,000 bit image. Since I am now
displaying
the image on a form on by screen at about 1100 X 850 pixels, (935,000
bits),
interpolation must be used along with the scaling to get a good image. I
am
pretty sure the scaling and interpolation is what is taking up most of the
time as this is done at every paint event.

It seems to me that there should be some way to scale, interpolate, and
draw
the image to a buffer that is saved. Then at the paint event, the buffer
could be read and drawn without having to go through the scaling and
interpolation every time.


You can draw the Bitmap from file into a memory Bitmap and then paint the
memory Bitmap inside onpaint, eg.:

Public Filename as String
Dim bm As Bitmap ' memory bitmap

Public Sub DrawTif()
Dim fileBitmap As Bitmap = Bitmap.FromFile(FileName)

' create memory bitmap
Dim gfx As Graphics = CreateGraphics()
bm = New Bitmap( 400, 500, gfx )
gfx.Dispose()

' draw file-bitmap scaled into memory-bitmap
gfx = Graphics.FromImage( bm )
gfx.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
gfx.DrawImage(fileBitmap, New Rectangle(0, 0, 400, 500))
gfx.Dispose()

' dispose file bitmap
fileBitmap.Dispose();

' trigger onpaint
Invalidate()
End Sub

Sub Page1_paint
If ( Not bm Is Nothing ) Then
e.Graphics.DrawImageUnscaled(bm, 0, 0, 0, 0, 0 )
End If
End Sub

Sub Page1_Closing
bm.Dispose
bm = Nothing
End Sub

HTH,
Greetings


Any thoughts here? Maybe this cannot be done in VB.

Hamil.

Nov 21 '05 #4
If it's a large bitmap, wouldn't using BitBlt to copy the bitmap to the
graphics object passed in the paint event be much faster instead of drawimage?
--
Dennis in Houston
"Bart Mermuys" wrote:
Hi,

"hamil" <ha***@discussions.microsoft.com> wrote in message
news:D4**********************************@microsof t.com...


"Bart Mermuys" wrote:
Hi,

"hamil" <ha***@discussions.microsoft.com> wrote in message
news:ED**********************************@microsof t.com...
> The following code will display a tif file on a form. When another form
> is
> moved over the tif image, the tif image is erased where the form was
> moved.
> A paint event occurs when this happens. My question is..how would I
> save
> the
> tif images as soon as it is initially painted, and then restore the
> image
> in
> a paint event. It seems to me that there should be some sort of a
> SaveImage

But why would you want to save and restore it, it is already in a Bitmap,
just (re)draw it inside OnPaint. Also, never store a Graphics object
obtained with CreateGraphics, you must always Dispose it before the
eventhandler finishes.

See code below.

> method and a RestoreImage method that would be fast. Note: Page1 is the
> form
> name, and FileName is passed from a routine on another form.

Public Filename as String
Dim bm As Bitmap

Public Sub DrawTif()
bm = Bitmap.FromFile(FileName)
End Sub

Sub Page1_paint
If ( Not bm Is Nothing ) Then
e.Graphics.InterpolationMode =
Drawing2D.InterpolationMode.HighQualityBicubic
e.DrawImage(bm, New Rectangle(0, 0, 400, 500))
End If
End sub

Sub Page1_Closing
bm.Dispose
bm = Nothing
End Sub

HTH,
Greetings

Bert..

Your code works, but the paint event is slow. My image is a tif that was
originally an 8-1/2 X 11 page, black-white sheet scanned at 300 DPI, that
results in about 8,200,000 pixels. I use a Group four Fax compression that
makes the file about 50K bytes. The code " bm = bitmap.from file..." will
decompress and regenerate the 8,200,000 bit image. Since I am now
displaying
the image on a form on by screen at about 1100 X 850 pixels, (935,000
bits),
interpolation must be used along with the scaling to get a good image. I
am
pretty sure the scaling and interpolation is what is taking up most of the
time as this is done at every paint event.

It seems to me that there should be some way to scale, interpolate, and
draw
the image to a buffer that is saved. Then at the paint event, the buffer
could be read and drawn without having to go through the scaling and
interpolation every time.


You can draw the Bitmap from file into a memory Bitmap and then paint the
memory Bitmap inside onpaint, eg.:

Public Filename as String
Dim bm As Bitmap ' memory bitmap

Public Sub DrawTif()
Dim fileBitmap As Bitmap = Bitmap.FromFile(FileName)

' create memory bitmap
Dim gfx As Graphics = CreateGraphics()
bm = New Bitmap( 400, 500, gfx )
gfx.Dispose()

' draw file-bitmap scaled into memory-bitmap
gfx = Graphics.FromImage( bm )
gfx.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
gfx.DrawImage(fileBitmap, New Rectangle(0, 0, 400, 500))
gfx.Dispose()

' dispose file bitmap
fileBitmap.Dispose();

' trigger onpaint
Invalidate()
End Sub

Sub Page1_paint
If ( Not bm Is Nothing ) Then
e.Graphics.DrawImageUnscaled(bm, 0, 0, 0, 0, 0 )
End If
End Sub

Sub Page1_Closing
bm.Dispose
bm = Nothing
End Sub

HTH,
Greetings


Any thoughts here? Maybe this cannot be done in VB.

Hamil.


Nov 21 '05 #5
Hi,

"Dennis" <De****@discussions.microsoft.com> wrote in message
news:3A**********************************@microsof t.com...
If it's a large bitmap, wouldn't using BitBlt to copy the bitmap to the
graphics object passed in the paint event be much faster instead of
drawimage?
BitBlt is GDI and DrawImage is GDI+, BitBlt will not scale but i guess you
could use it to transfer the memory Bitmap to the screen and maybe it will
be faster (i've heard GDI is faster then GDI+). Haven't tried it though.
HTH,
Greetings
--
Dennis in Houston
"Bart Mermuys" wrote:
Hi,

"hamil" <ha***@discussions.microsoft.com> wrote in message
news:D4**********************************@microsof t.com...
>
>
> "Bart Mermuys" wrote:
>
>> Hi,
>>
>> "hamil" <ha***@discussions.microsoft.com> wrote in message
>> news:ED**********************************@microsof t.com...
>> > The following code will display a tif file on a form. When another
>> > form
>> > is
>> > moved over the tif image, the tif image is erased where the form was
>> > moved.
>> > A paint event occurs when this happens. My question is..how would I
>> > save
>> > the
>> > tif images as soon as it is initially painted, and then restore the
>> > image
>> > in
>> > a paint event. It seems to me that there should be some sort of a
>> > SaveImage
>>
>> But why would you want to save and restore it, it is already in a
>> Bitmap,
>> just (re)draw it inside OnPaint. Also, never store a Graphics object
>> obtained with CreateGraphics, you must always Dispose it before the
>> eventhandler finishes.
>>
>> See code below.
>>
>> > method and a RestoreImage method that would be fast. Note: Page1 is
>> > the
>> > form
>> > name, and FileName is passed from a routine on another form.
>>
>> Public Filename as String
>> Dim bm As Bitmap
>>
>> Public Sub DrawTif()
>> bm = Bitmap.FromFile(FileName)
>> End Sub
>>
>> Sub Page1_paint
>> If ( Not bm Is Nothing ) Then
>> e.Graphics.InterpolationMode =
>> Drawing2D.InterpolationMode.HighQualityBicubic
>> e.DrawImage(bm, New Rectangle(0, 0, 400, 500))
>> End If
>> End sub
>>
>> Sub Page1_Closing
>> bm.Dispose
>> bm = Nothing
>> End Sub
>>
>> HTH,
>> Greetings
>>
>>
>>
> Bert..
>
> Your code works, but the paint event is slow. My image is a tif that
> was
> originally an 8-1/2 X 11 page, black-white sheet scanned at 300 DPI,
> that
> results in about 8,200,000 pixels. I use a Group four Fax compression
> that
> makes the file about 50K bytes. The code " bm = bitmap.from file..."
> will
> decompress and regenerate the 8,200,000 bit image. Since I am now
> displaying
> the image on a form on by screen at about 1100 X 850 pixels, (935,000
> bits),
> interpolation must be used along with the scaling to get a good image.
> I
> am
> pretty sure the scaling and interpolation is what is taking up most of
> the
> time as this is done at every paint event.
>
> It seems to me that there should be some way to scale, interpolate, and
> draw
> the image to a buffer that is saved. Then at the paint event, the
> buffer
> could be read and drawn without having to go through the scaling and
> interpolation every time.


You can draw the Bitmap from file into a memory Bitmap and then paint the
memory Bitmap inside onpaint, eg.:

Public Filename as String
Dim bm As Bitmap ' memory bitmap

Public Sub DrawTif()
Dim fileBitmap As Bitmap = Bitmap.FromFile(FileName)

' create memory bitmap
Dim gfx As Graphics = CreateGraphics()
bm = New Bitmap( 400, 500, gfx )
gfx.Dispose()

' draw file-bitmap scaled into memory-bitmap
gfx = Graphics.FromImage( bm )
gfx.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
gfx.DrawImage(fileBitmap, New Rectangle(0, 0, 400, 500))
gfx.Dispose()

' dispose file bitmap
fileBitmap.Dispose();

' trigger onpaint
Invalidate()
End Sub

Sub Page1_paint
If ( Not bm Is Nothing ) Then
e.Graphics.DrawImageUnscaled(bm, 0, 0, 0, 0, 0 )
End If
End Sub

Sub Page1_Closing
bm.Dispose
bm = Nothing
End Sub

HTH,
Greetings

>
> Any thoughts here? Maybe this cannot be done in VB.
>
> Hamil.
>


Nov 21 '05 #6
Hi,

<snipped>
It seems to me that there should be some way to scale, interpolate, and
draw
the image to a buffer that is saved. Then at the paint event, the buffer
could be read and drawn without having to go through the scaling and
interpolation every time.
You can draw the Bitmap from file into a memory Bitmap and then paint the
memory Bitmap inside onpaint, eg.:

Public Filename as String
Dim bm As Bitmap ' memory bitmap

Public Sub DrawTif()
Dim fileBitmap As Bitmap = Bitmap.FromFile(FileName)

' create memory bitmap
Dim gfx As Graphics = CreateGraphics()
bm = New Bitmap( 400, 500, gfx )
gfx.Dispose()

' draw file-bitmap scaled into memory-bitmap
gfx = Graphics.FromImage( bm )
gfx.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
gfx.DrawImage(fileBitmap, New Rectangle(0, 0, 400, 500))
gfx.Dispose()

' dispose file bitmap
fileBitmap.Dispose();

' trigger onpaint
Invalidate()
End Sub


Because Dennis asked something about the speed of DrawImage, i was thinking
that OnPaint could be faster if you do not draw the whole memory Bitmap but
just the invalidated part.

Sub Page1_paint
If ( Not bm Is Nothing ) Then
e.Graphics.DrawImage(bm, e.ClipRectangle, e.ClipRectangle,
GraphicsUnit.Pixel)
End If
End Sub

HTH,
Greetings

Sub Page1_Closing
bm.Dispose
bm = Nothing
End Sub

HTH,
Greetings


Any thoughts here? Maybe this cannot be done in VB.

Hamil.


Nov 21 '05 #7
Thanks to both of you..

Barts first suggestion was much better than the way I was doing theonPaint.
Barts second suggestion was even better.

I looked into the BitBlt approach and I think it might be even faster but I
think this method would require me operating "outside" of VB.net exclusively,
i.e. require a Win API call.
Barts second suggeston where only the required part is repainted works very
well even on my slow 1 GHz Pentium III.

Thanks again.

Hamil.


"hamil" wrote:
The following code will display a tif file on a form. When another form is
moved over the tif image, the tif image is erased where the form was moved.
A paint event occurs when this happens. My question is..how would I save the
tif images as soon as it is initially painted, and then restore the image in
a paint event. It seems to me that there should be some sort of a SaveImage
method and a RestoreImage method that would be fast. Note: Page1 is the form
name, and FileName is passed from a routine on another form.

Public Filename as string
dim gr as graphics

Public Sub DrawTif()
Dim bm As Bitmap

bm = Bitmap.FromFile(FileName)

gr = Me.CreateGraphics
gr.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
gr.DrawImage(bm, New Rectangle(0, 0, 400, 500))
>>> can I save the image here End Sub

sub Page1_paint>> and restore it here

end sub

sub Page1_Closing
gr.dispose
end sub

Nov 21 '05 #8

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

Similar topics

0
by: DraguVaso | last post by:
Hi, I 'wrote' my own DataGrid, using the normal DataGrid, and overriding some of the DataGridTextBoxColumn-methods (mostly the Paint-method). I based most of my changes on the Windows Form FAQ:...
5
by: Tamir Khason | last post by:
How I can paint something onClick event? I need PaintEventArgs event in order to paint and raise Graphics, but onClick I have only EventArgs Event... Thanx
0
by: vooose | last post by:
Consider a UserControl to which you do userControl.Paint += new PaintEventHandler(paint_method) If you don't like that way, and prefer to override onPaint( ) then the problem stated below...
3
by: Mike Cooper | last post by:
I have been staring at the above error for over a week now! I have a an inherited data class looking like thus: Public Class DataGridBoolColumnInherit Inherits...
8
by: WStoreyII | last post by:
i am using this code to try to paint a line on my form and it wont work i get an object not set to a reference error Dim myPen As New System.Drawing.Pen(System.Drawing.Color.Red) Dim...
6
by: jcrouse | last post by:
I am rotating some text is some label controls. In the one place I use it it works fine. In the other place I use it I can't figure out the syntax. I don't really understand the event. Where it...
4
by: Sam | last post by:
Hai, I use a paintbox paint event to draw some images in a paintbox. I call the paint event by refreshing the paintbox using paintboxname.refresh() or paintboxname.update(). Problem with this...
7
by: Rotsey | last post by:
Hi, I have a interface that I use for a form so I can pass the form to another object. How do I add the Paint event to the interface and subsequently handle the paint event in my other...
5
by: =?Utf-8?B?SmVzcGVyLCBEZW5tYXJr?= | last post by:
Hi, On a usercontrol I've put a set of radiobuttons within a groupbox. These radiobuttons have visual style enables, i.e. they turn orange when hovering over them and green when pushed. ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.