building up a bitmap

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a bitmap of 100X100. On the load, the bitmap is created by a function
(createimage()). On my OnPaint, I draw the image back to the screen
(e.Graphics.DrawImage( bitmap, destrect)).

Now, I want to add a button. When user click on it, I want the bitmap shift
to the left by 10% and fill the new area with new information requested.
Here is the code I have in the button handler:
if (bitmap == null) return;
Graphics graphics = Graphics.FromImage(bitmap);
Matrix X = new Matrix();
X.Translate(10, 0);
graphics.Transform = X;
Pen myPen = new Pen(Color.Red, 1);
graphics.DrawLine(myPen, 90, 0, 100, 100);
graphics.Dispose();
Invalidate();
The problem here is that I don't see any translation of my bitmap. Can
someone point me where the problem is?

Thanks in advance,
Anthony
 
Oh! How do I transform the whole bitmap then ? If using Matrix cannot do
it, can you point me how I can shift my bitmap?
 
You'd have to also draw the bitmap.

Something like (aircode):
if (bitmap == null) return;
Bitmap newBitmap = new bitmap(bitmap.Width, bitmap.Height);
using(Graphics graphics = Graphics.FromImage(newBitmap))
{
Matrix X = new Matrix();
X.Translate(10, 0);
graphics.Transform = X;
Pen myPen = new Pen(Color.Red, 1);
graphics.DrawLine(myPen, 90, 0, 100, 100);
graphics.DrawImage(bitmap, 0,0);
}
bitmap = newBitmap;
Invalidate();

If you using a PictureBox, you'll have to re-assign the bitmap property,
unless you use bitmap as the destination (Graphics.FromImage) and load the
bitmap you're drawing into a new bitmap object.
--
Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#
 
that works! Thank you, Peter.

Things considered. I don't need to use the Matrix since I am doing a simple
translation. I can have the save effect using the drawImage with its
parameters. I thought I don't have to build a second bitmap which is a
resource waste.

Anthony.
 

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

Back
Top