By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,611 Members | 1,642 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,611 IT Pros & Developers. It's quick & easy.

Graphics

P: n/a
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
When i CreateGraphics() From the Standard CreateGraphics() function the code
below works
perfect..
when i CreateGraphics.FromImage()
the Resuting Bitmap is Not Copied Right.... i think. when i assign the
bitmap to a picturebox or button its not correct
can anybody tell me what the problem is here

int Id=10;
Rectangle Rect = new Rectangle(new Point(Id*32, 0), new Size(32,
32));
Bitmap bmp = new Bitmap(Rect.Width, Rect.Height);

//Graphics g = Graphics.FromImage(bmp); // draws to
bitmap but is all messed up
Graphics g = CreateGraphics(); //
works perfect with this but draws to the window at 0,0 i want a bitmap back
g.DrawImage(Source, 0, 0, Rect, GraphicsUnit.Pixel);
g.Dispose();
Thanks DaveL
Oct 23 '08 #1
Share this Question
Share on Google+
9 Replies


P: n/a
"DaveL" <dv*****@sbcglobal.netwrote in message
news:6E*****************@nlpi064.nbdc.sbc.com...
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
When i CreateGraphics() From the Standard CreateGraphics() function the
code below works
perfect..
when i CreateGraphics.FromImage()
the Resuting Bitmap is Not Copied Right.... i think. when i assign the
bitmap to a picturebox or button its not correct
can anybody tell me what the problem is here

int Id=10;
Rectangle Rect = new Rectangle(new Point(Id*32, 0), new Size(32,
32));
Bitmap bmp = new Bitmap(Rect.Width, Rect.Height);

//Graphics g = Graphics.FromImage(bmp); // draws to
bitmap but is all messed up
Graphics g = CreateGraphics(); //
works perfect with this but draws to the window at 0,0 i want a bitmap
back
g.DrawImage(Source, 0, 0, Rect, GraphicsUnit.Pixel);
g.Dispose();
You might want to repost in microsoft.public.dotnet.framework.drawing.
However, where in that posted code are you using the bmp variable? The only
place I see it is in the stuff you commented out. And where do you want to
draw the bitmap to? You'll need two Graphics objects: one for the bitmap
(the source) and one for the destination.
Oct 23 '08 #2

P: n/a
New bmp is created 32x32
CreateGraphics() draws to the form perfect
when i CreateGraphics.FromImage(bmp) // new bmp
then i use g.drawImage(Source < source bmp, 0,0,Rect,GraphicsUnit.Pixel);
// draws to new bmp but when i place bmp on control not correct
that is all below

"Jeff Johnson" <i.***@enough.spamwrote in message
news:OT*************@TK2MSFTNGP02.phx.gbl...
"DaveL" <dv*****@sbcglobal.netwrote in message
news:6E*****************@nlpi064.nbdc.sbc.com...
>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
When i CreateGraphics() From the Standard CreateGraphics() function the
code below works
perfect..
when i CreateGraphics.FromImage()
the Resuting Bitmap is Not Copied Right.... i think. when i assign the
bitmap to a picturebox or button its not correct
can anybody tell me what the problem is here

int Id=10;
Rectangle Rect = new Rectangle(new Point(Id*32, 0), new
Size(32, 32));
Bitmap bmp = new Bitmap(Rect.Width, Rect.Height);

//Graphics g = Graphics.FromImage(bmp); // draws to
bitmap but is all messed up
Graphics g = CreateGraphics(); //
works perfect with this but draws to the window at 0,0 i want a bitmap
back
g.DrawImage(Source, 0, 0, Rect, GraphicsUnit.Pixel);
g.Dispose();

You might want to repost in microsoft.public.dotnet.framework.drawing.
However, where in that posted code are you using the bmp variable? The
only place I see it is in the stuff you commented out. And where do you
want to draw the bitmap to? You'll need two Graphics objects: one for the
bitmap (the source) and one for the destination.

Oct 23 '08 #3

P: n/a
Lee
This function returns a bitmap that is a sub-section of the original

If you look at your code and this function you will see where your
thought process went awry.

Remember that if you create a graphics object from a bitmap, then
anything done to the graphics object is applied to your bitmap, so
after your g.DrawImage, all you needed to do was use your altered
bitmap.

I created a dummy 1367x32 image then an altered version of your code,
I itterated through id from 0 to 39 and set a picturebox image
property to the resulting bitmap. All looked fine.

The function below was found at: http://msdn.microsoft.com/en-us/library/aa457087.aspx

public Bitmap Copy(Bitmap srcBitmap, Rectangle section)
{
// Create the new bitmap and associated graphics object
Bitmap bmp = new Bitmap(section.Width, section.Height);
Graphics g = Graphics.FromImage(bmp);

// Draw the specified section of the source bitmap to the
new one
g.DrawImage(srcBitmap, 0, 0, section, GraphicsUnit.Pixel);

// Clean up
g.Dispose();

// Return the bitmap
return bmp;
}
}

L. Lee Saunders
http://oldschooldotnet.blogspot.com

On Oct 23, 11:30*am, "DaveL" <dvs_...@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

When i CreateGraphics() From the Standard CreateGraphics() function the code
below works
perfect..
when i CreateGraphics.FromImage()
the Resuting Bitmap is Not Copied Right.... i think. when i assign the
bitmap to a picturebox or button its not correct

can anybody tell me what the problem is here

* * * * * *int Id=10;
* * * * * *Rectangle Rect = new Rectangle(new Point(Id*32, 0), new Size(32,
32));
* * * * * *Bitmap bmp = new Bitmap(Rect.Width, Rect.Height);

* * * * * *//Graphics g = Graphics.FromImage(bmp); * * * * * // draws to
bitmap but is all messed up
* * * * * *Graphics g = CreateGraphics(); * * * * * * * * * * * * * * //
works perfect with this but draws to the window at 0,0 i want a bitmap back
* * * * * *g.DrawImage(Source, 0, 0, Rect, GraphicsUnit.Pixel);
* * * * * *g.Dispose();

Thanks DaveL
Oct 23 '08 #4

P: n/a
Lee, I am using my alterd bitmap
below is new code from that link you posted
i still get the same results, if i draw to screen perfect
if i draw to bitmap not perfect
private void button3_Click(object sender, EventArgs e)
{
Bitmap srcBitmap =
(Bitmap)Bitmap.FromFile(@"D:\_netdev\dev\pics\band 32.bmp");
Rectangle section = new Rectangle(16 * 32, 0, 32, 32);
// Create the new bitmap and associated graphics object
Bitmap bmp = new Bitmap(section.Width, section.Height);
Graphics g = Graphics.FromImage(bmp);

// Draw the specified section of the source bitmap to the new
one
g.DrawImage(srcBitmap, 0, 0, section, GraphicsUnit.Pixel);

// Clean up
g.Dispose();
this.button3.Image = bmp;

"Lee" <sa******@hotmail.comwrote in message
news:6f**********************************@m3g2000h sc.googlegroups.com...
This function returns a bitmap that is a sub-section of the original

If you look at your code and this function you will see where your
thought process went awry.

Remember that if you create a graphics object from a bitmap, then
anything done to the graphics object is applied to your bitmap, so
after your g.DrawImage, all you needed to do was use your altered
bitmap.

I created a dummy 1367x32 image then an altered version of your code,
I itterated through id from 0 to 39 and set a picturebox image
property to the resulting bitmap. All looked fine.

The function below was found at:
http://msdn.microsoft.com/en-us/library/aa457087.aspx

public Bitmap Copy(Bitmap srcBitmap, Rectangle section)
{
// Create the new bitmap and associated graphics object
Bitmap bmp = new Bitmap(section.Width, section.Height);
Graphics g = Graphics.FromImage(bmp);

// Draw the specified section of the source bitmap to the
new one
g.DrawImage(srcBitmap, 0, 0, section, GraphicsUnit.Pixel);

// Clean up
g.Dispose();

// Return the bitmap
return bmp;
}
}

L. Lee Saunders
http://oldschooldotnet.blogspot.com

On Oct 23, 11:30 am, "DaveL" <dvs_...@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

When i CreateGraphics() From the Standard CreateGraphics() function the
code
below works
perfect..
when i CreateGraphics.FromImage()
the Resuting Bitmap is Not Copied Right.... i think. when i assign the
bitmap to a picturebox or button its not correct

can anybody tell me what the problem is here

int Id=10;
Rectangle Rect = new Rectangle(new Point(Id*32, 0), new Size(32,
32));
Bitmap bmp = new Bitmap(Rect.Width, Rect.Height);

//Graphics g = Graphics.FromImage(bmp); // draws to
bitmap but is all messed up
Graphics g = CreateGraphics(); //
works perfect with this but draws to the window at 0,0 i want a bitmap
back
g.DrawImage(Source, 0, 0, Rect, GraphicsUnit.Pixel);
g.Dispose();

Thanks DaveL

Oct 23 '08 #5

P: n/a
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;
}
}
}
Oct 23 '08 #6

P: n/a
"DaveL" <dv*****@sbcglobal.netwrote in message
news:Qf*****************@nlpi067.nbdc.sbc.com...
Lee, I am using my alterd bitmap
below is new code from that link you posted
i still get the same results, if i draw to screen perfect
if i draw to bitmap not perfect
private void button3_Click(object sender, EventArgs e)
{
Bitmap srcBitmap =
(Bitmap)Bitmap.FromFile(@"D:\_netdev\dev\pics\band 32.bmp");
Rectangle section = new Rectangle(16 * 32, 0, 32, 32);
// Create the new bitmap and associated graphics object
Bitmap bmp = new Bitmap(section.Width, section.Height);
Graphics g = Graphics.FromImage(bmp);

// Draw the specified section of the source bitmap to the new
one
g.DrawImage(srcBitmap, 0, 0, section, GraphicsUnit.Pixel);

// Clean up
g.Dispose();
this.button3.Image = bmp;
You probably need to set the PixelFormat of the Bitmap to the proper value.
You may be losing resolution.

But so far you've said "it's wrong" and it's "not perfect." This is not
giving us much information on what's REALLY wrong.
Oct 23 '08 #7

P: n/a
Lee
Your image is in a lower DPI (76) than screen dpi (96). It is an
issue with Bitmaps. If you saved it as a PNG, it would look fine.
Otherwise, sections to the right and bottom are getting cut off.

I changed the DPI of the image to 96 and now it works just fine.

L. Lee Saunders
http://oldschooldotnet.blogspot.com
Oct 23 '08 #8

P: n/a
when graphics draws to the screen
its correct 32x32 same as in the original bitmap
just the 32x32 portion

when i use graphics created from the new bitmap
i get somewhat the correct area of the original bitmap
but its not correct
I really dont know who to explain this problem

one way works fine , the other does not work correctly

Dave

"Jeff Johnson" <i.***@enough.spamwrote in message
news:uy**************@TK2MSFTNGP05.phx.gbl...
"DaveL" <dv*****@sbcglobal.netwrote in message
news:Qf*****************@nlpi067.nbdc.sbc.com...
>Lee, I am using my alterd bitmap
below is new code from that link you posted
i still get the same results, if i draw to screen perfect
if i draw to bitmap not perfect
private void button3_Click(object sender, EventArgs e)
{
Bitmap srcBitmap =
(Bitmap)Bitmap.FromFile(@"D:\_netdev\dev\pics\ban d32.bmp");
Rectangle section = new Rectangle(16 * 32, 0, 32, 32);
// Create the new bitmap and associated graphics object
Bitmap bmp = new Bitmap(section.Width, section.Height);
Graphics g = Graphics.FromImage(bmp);

// Draw the specified section of the source bitmap to the new
one
g.DrawImage(srcBitmap, 0, 0, section, GraphicsUnit.Pixel);

// Clean up
g.Dispose();
this.button3.Image = bmp;

You probably need to set the PixelFormat of the Bitmap to the proper
value. You may be losing resolution.

But so far you've said "it's wrong" and it's "not perfect." This is not
giving us much information on what's REALLY wrong.

Oct 23 '08 #9

P: n/a
Lee
I was wondering about that,
I did look into the resolution, but did
not know how to change it

Just like to thank you very much
for your time...

DaveP

"Lee" <sa******@hotmail.comwrote in message
news:a4**********************************@k16g2000 hsf.googlegroups.com...
Your image is in a lower DPI (76) than screen dpi (96). It is an
issue with Bitmaps. If you saved it as a PNG, it would look fine.
Otherwise, sections to the right and bottom are getting cut off.

I changed the DPI of the image to 96 and now it works just fine.

L. Lee Saunders
http://oldschooldotnet.blogspot.com

Oct 23 '08 #10

This discussion thread is closed

Replies have been disabled for this discussion.