# [.net] C# CF3.5 Cannot get portion of image to draw using DrawImage()

## Recommended Posts

Hi, guys! Just joined up after lurking for a while. I have limited programming experience, so please bear with me :) I'm trying to build a simple C# game for my mobile phone, so I'm using .net CF 3.5 on my Touch HD (WM 6.1). The basic game works, but now I'm trying to add a score counter using numbers from a texture sheet I made. Looking around, this should be simple: just use an e.Graphics.DrawImage() call in the counter's OnPaint (the counter is a custom controll derived from UserControl), using the (bitmap, destRect, scrRect, gfxUnit) overload. This should do the trick and MSDN tells me this call is supported in CF 3.5. Everything seems to work (the incrementing to change the scrRect to crop the number I want, the placement of the counter), but for some reason the actual bitmap-part won't show! It just displays the background :( Oddly enough, to troubleshoot, I placed a "e.Graphics.Clear(Color.Beige);" just above the DrawImage() in the control's OnPaint, and now, exactly where I would want to see a nice bit of bitmap containing my cropped number, I see a beige box :( Here's the counter class:
sing System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Drawing;

namespace Mash2

public class SideScrollScoreBox : System.Windows.Forms.UserControl
{
//vars
public Bitmap scoreSheet;
public Rectangle bitmapPart;
public Rectangle locRect;

//accessors
protected override void OnPaintBackground(System.Windows.Forms.PaintEventArgs e)
{
//Do nothing
}
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
e.Graphics.Clear(Color.Beige);
e.Graphics.DrawImage(scoreSheet, locRect, bitmapPart, GraphicsUnit.Pixel);
}

//constructors
public SideScrollScoreBox(Bitmap scoreSheet)
{
this.scoreSheet = scoreSheet;
}
}
}

One of the counters is initialised like so (in the form):
private static Bitmap blueNumbersJ = new Bitmap(@"Program Files\\Mash2\\jpgs\\numbersheet_blue.jpg");
public SideScrollScoreBox redScoreBox = new SideScrollScoreBox(redNumbersJ);

with it being set up further in a method DrawBackground():
//define lower picbox scoreboard
//SideScrollScoreBox redScoreBox = new SideScrollScoreBox(redNumbersJ); //done as public static uptop
redScoreBox.Location = new Point(23, 703);
redScoreBox.Size = new Size(70, 70);
redScoreBox.bitmapPart = new Rectangle(0, 0, 48, 64);
redScoreBox.locRect = new Rectangle(redScoreBox.Location.X, redScoreBox.Location.Y, redScoreBox.Size.Width, redScoreBox.Size.Height);
this.Controls.SetChildIndex(redScoreBox, 4);
redScoreBox.BringToFront();

There's some logic to move along the .X of bitmapPart, but that seems to work fine. The problem is that the DrawImage() call doesn't seem to work. Is this a known problem (ie cf3.5 can't crop images, despite what MSDN says) or am I doing something dramatically wrong? PS: how do you get those nice codeboxes? <code></code> just doesn't look as nice!

##### Share on other sites
Co-ordinates are relative to the control you got your Graphics from (if not explicitly changed through Transform matrix), so in the Paint handler, the co-ordinate (0,0) is actually top left of your SideScrollScoreBox. But you are calling DrawImage with a destRect which was initialized through the location new Point(23, 703). So you are probably off the (visible) screen/painting area. Painting seems to work, since the clear color matches.

Haven't used CF and WM but Windows XP, maybe there's something different with the co-ordinate system (origin at the bottom left ?). I doubt that, but honestly I don't know.

Play around with the destRect parameter of DrawImage, e.g. something like new Rectangle(0,0, redScoreBox.Size.Width, redScoreBox.Size.Height), and see if you get something. Also try using even simpler drawing calls (FillRect) to get familiar with the coordinates.

Hope that helps.

##### Share on other sites
*thud*

*thud*

*thud*

That's the sound of my head hitting my desk.

You were absolutely right about where I was pointing my destRect...I thought I was being clever with the programatic referencing, and I'd done just about everything to troubleshoot this, but everything seemed correct, the bitmap was displaying correctly in a test picbox, the increments were incrementing properly....

Sigh :)

Thank you bundles for looking over my code ... I was just blind from looking over it myself :)

Now to add animation and I'll have a nice little moving score counter :)

##### Share on other sites
Quote:
 *thud*

[smile]Yeah, sometimes one just can't see the most obvious things. Then it's time for a break - or sleep. Glad I could help.
Quote:
 PS: how do you get those nice codeboxes? just doesn't look as nice!

  [ source]   Like this; // omit the spaces, though   [ /source]

Better explained explained here, and here.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
627735
• Total Posts
2978855

• 10
• 10
• 21
• 14
• 12