newbie and DrawImage

D

Dan

My application has a form which should display an image at an arbitrary zoom
factor, and optionally draw other images (like layers in Photoshop) onto the
'base' image. The easiest way I can think of could be e.g.:

----OnPaint handler:
// keep into account scrollbar position
g.TranslateTransform(AutoScrollPosition.X, AutoScrollPosition.Y);

// calculate the destination rectangle scaled according to _fScale
(1.0=100%, 0.5=50%, etc)
Rectangle rctImage = new Rectangle(0, 0,
(int)(_bmpBase.Width * _fScale), (int)(_bmpBase.Height * _fScale));

// draw the base image
g.DrawImage(_bmpBase, rctImage);

// draw layers onto it with opacity value _fOpacity (1=100%)
foreach (bmp in layersCollection)
{
ColorMatrix cm = new ColorMatrix();
cm.Matrix33 = _fOpacity;
ImageAttributes ia = new ImageAttributes();
ia.SetColorMatrix(cm, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
g.DrawImage(bmp, rctImage,
0, 0,
_bmpBase.Width, _bmpBase.Height,
GraphicsUnit.Pixel, ia);
}

This works, but cannot be an optimal way of doing it, as the program slows
down when the image is big (e.g. 2000x2000 pixels). I thought I could gain
performance by drawing not the whole image(s) but only the portion required
for display. Thus I have written this demo which just shows an image onto a
form using a smaller source rectangle:

Rectangle rctSrc = new Rectangle(
Math.Abs(AutoScrollPosition.X), Math.Abs(AutoScrollPosition.Y),
(int)((float)ClientSize.Width / _fScale),
(int)((float)ClientSize.Height / _fScale));

g.DrawImage(_bmp, ClientRectangle, rctSrc.X, rctSrc.Y, rctSrc.Width,
rctSrc.Height,
GraphicsUnit.Pixel);

This works BUT when scrolling with the 'arrows' (not dragging the thumb)
creates display errors on the sides opposite to the direction of scrolling.
If I minimize and then restore the form this problem is fixed, so it must be
related to the scrolling logic when scrolling 'smoothly'. How can I fix
this? And more generally, how can I gain true performance when drawing
'overlapped' images? My attempt could be effective, or I should just think
of other ways for optimizing?

Thanx to all!
 
D

Dan

Thanks, nice site! Anyway, the article does not respond to my question, that
is, it shows an example of image drawing where DrawImage is simply called to
show the whole image. I don't know if reducing the source image rectangle
could be beneficial, anyway I cannot go this way unless I manage to solve
the scroll issue I was talking about. Or is there any effective technique to
speed up image drawing in my case?
 
S

Supra

to show whole image. u will have to create another procedure events. i
used tow procedure events one for small and and one for whole image. but
i used user control.
regards
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top