Bitmap Zoom Problem

Hi Friends,

I have written code for zooming bitmap and mouse drawing. The code is given below. Everything is working fine. But I find there is a strange behaviour like, when I feed the bitmap size 20 x 20 pixels, the zoomed bitmap shows only 19.5 x 19.5. Where is the half pixel is gone, else why the half pixel is not shown like other pixels. The first x pixel and first y pixel turned to be with this problem. I want to display all the pixels with equal size. Please help me. Also advise me how to avoid flickering when the image is drawn on the panel.

Code:

sing System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Drawing.Drawing2D;

using System.Drawing.Design;

namespace WindowsApplication1

{

publicpartialclassForm1 :Form

{

Bitmap bmp;

Graphics gp;

Point prePoint, curPoint;

Pen pen;

public Form1()

{

InitializeComponent();

bmp =newBitmap(10, 10);

gp =Graphics.FromImage(bmp);

pen =newPen(Color.Red);

this.DoubleBuffered =true;

}

privatevoid panel1_MouseDown(object sender,MouseEventArgs e)

{

if (e.Button ==MouseButtons.Left)

prePoint =newPoint((1 + e.X / 10), (1 + e.Y / 10));

}

privatevoid panel1_MouseMove(object sender,MouseEventArgs e)

{

if (e.Button ==MouseButtons.Left)

curPoint =newPoint((1 + e.X / 10), (1 + e.Y / 10));

draw(e);

prePoint = curPoint;

this.panel1.Invalidate();

}

privatevoid draw(MouseEventArgs e)

{

gp.DrawLine(pen, curPoint, prePoint);

}

privatevoid panel1_Paint(object sender,PaintEventArgs e)

{

Graphics gpp = e.Graphics;

gpp.InterpolationMode =InterpolationMode.NearestNeighbor;

gpp.ScaleTransform(10.0f, 10.0f);

gpp.DrawImage(bmp, 0, 0);

}

}

}

[5161 byte] By [RR_14] at [2008-1-9]
# 1
You'll need to turn on double-buffer on the panel, not the form, to avoid the flicker. Add a new class to your project, paste the code shown below. Build, then use the new control at the top of the toolbox instead of a regular panel.

using System;
using System.Windows.Forms;

public class MyPanel : Panel {
public MyPanel() {
this.DoubleBuffered = true;
}
}

Solve the half-pixel problem with this statement in your Paint event handler:

gpp.PixelOffsetMode = PixelOffsetMode.HighQuality;

nobugz at 2007-10-3 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 2

Hi Nobugz,

Thanks for your immediate reply, the half pixel problem is solved. Thanks lot. But I could not get the flickering. As you have described, I have added new class to the project and followed the instruction, but the flickering persists. Also please advise me, can I directly draw on to the form. Because I am developing a CAD software with much more drawing capabilities. Please advise.

RR_14 at 2007-10-3 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 3
Yes, you can directly draw on the form instead of using the panel.
nobugz at 2007-10-3 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 4

Hi,

In the abve example I want to add tools on panel, but the same flickerin appears. But I am not getting how to add the answer you have given. Please describe more.

Than you

RR_14 at 2007-10-3 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 5
Hi NOBUGZ,
The half pixel problem is solved, but I could not get out of the Panel flickering. As you have described I have created new class and pasted the codes:

using System;
using System.Windows.Forms;

public class MyPanel : Panel {
public MyPanel() {
this.DoubleBuffered = true;
}
}

But the same flickering persists. Please help me to solve this problem.

RR_14 at 2007-10-3 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 6
It didn't flicker when I tested it, can't really guess what might have gone wrong on your end. Did you actually use a MyPanel instead of a regular Panel?
nobugz at 2007-10-3 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 7

As you have mentioned I added a new class to the project and named it as MyPanel and added the code as you have mentioned. After compiling the same problem persists. Please help me.

RR_14 at 2007-10-3 > top of Msdn Tech,Windows Forms,Windows Forms General...