On Thu, 23 Oct 2008 09:30:13 -0700, DaveL <dv*****@sbcglobal.netwrote:
hello
I have a Bit map 1367 wide 32 high
this bitmap contains like 40 separate Images
32x32
I tell it the id*32 to get the approiate Image from
the source Bitmap
Note that 1367 isn't an even multiple of 32, never mind is it 40 * 32.
As far as your actual question goes, the code you posted looks fine so far
as it goes. But, it's not a concise-but-complete code sample, so there's
no way to know exactly what's going wrong. For sure, the general
technique you're describing works fine, when you've done everything right.
I've attached below a (mostly) concise-but-complete code sample that
demonstrates the technique you're trying to accomplish. My code works
fine. If you want help with your code, you need to post a
concise-but-complete code sample that _doesn't_ work fine.
Note: I say "mostly", because I left out the Designer-provided stuff; it's
a little more concise at the expense of being truly complete. In this
case, I think that's fine because I didn't change anything at all with
respect to the defaults. You can create a blank Forms application and
just copy-and-paste my code into the Form1.cs file and it will work. You
should be able to provide a similar concise-but-complete code sample.
Pete
using System;
using System.Drawing;
using System.Windows.Forms;
namespace TestDrawToBitmap
{
public partial class Form1 : Form
{
private const int _kiimgMax = 40;
private Image _imgSource;
private Image _imgCurrent;
private Random _rnd = new Random();
public Form1()
{
InitializeComponent();
_imgSource = _ImageInitSource();
_imgCurrent = _ImageNextRandom();
Timer timer = new Timer();
timer.Tick += _TickHandler;
timer.Interval = 1000;
timer.Start();
}
protected override void OnFormClosed(FormClosedEventArgs e)
{
_imgCurrent.Dispose();
_imgSource.Dispose();
base.OnFormClosed(e);
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.DrawImage(_imgCurrent, (ClientRectangle.Width -
_imgCurrent.Width) / 2, (ClientRectangle.Height - _imgCurrent.Height) / 2);
}
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
Invalidate();
}
private void _TickHandler(object sender, EventArgs e)
{
_imgCurrent.Dispose();
_imgCurrent = _ImageNextRandom();
Invalidate();
}
private Image _ImageNextRandom()
{
int cxImage = _imgSource.Width / _kiimgMax;
Point ptSource = new Point(_rnd.Next(_kiimgMax) * cxImage, 0);
Image imgRet = new Bitmap(cxImage, _imgSource.Height);
using (Graphics gfx = Graphics.FromImage(imgRet))
{
gfx.DrawImage(_imgSource, 0, 0, new Rectangle(ptSource,
imgRet.Size), GraphicsUnit.Pixel);
}
return imgRet;
}
private Image _ImageInitSource()
{
SizeF sizeText;
using (Image img = new Bitmap(1, 1))
using (Graphics gfx = Graphics.FromImage(img))
{
sizeText = gfx.MeasureString("00", Font);
}
int cxText = (int)(sizeText.Width + 0.5f),
cyText = (int)(sizeText.Height + 0.5f);
Image imgRet = new Bitmap(_kiimgMax * cxText, cyText);
StringFormat format = StringFormat.GenericDefault;
format.Alignment = StringAlignment.Center;
format.LineAlignment = StringAlignment.Center;
format.Trimming = StringTrimming.None;
format.FormatFlags |= StringFormatFlags.NoWrap;
using (Brush brushForeground = new SolidBrush(ForeColor))
using (Graphics gfx = Graphics.FromImage(imgRet))
{
gfx.Clear(BackColor);
for (int i = 0; i < _kiimgMax; i++)
{
string strText = i.ToString("00");
RectangleF rectLayout = new RectangleF(new PointF(i *
cxText, 0), new SizeF(cxText, cyText));
gfx.DrawString(strText, Font, brushForeground,
rectLayout, format);
}
}
return imgRet;
}
}
}