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!
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!