473,320 Members | 2,054 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Graphics

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
9 2607
"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
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
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
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
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
"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: JBiagio | last post by:
Hello All, I am attempting to learn a bit about the GDI+ transforms and charting data and I feel like I'm getting a handle on how the transforms work. My chart object has a large "canvas" bitmap...
12
by: Sanjay | last post by:
hi, We are currently porting our project from VB6 to VB .NET. Earlier we used to make scale transformations on objects like pictureBox , forms etc.Now Such transformations are made on the...
2
by: John Bailo | last post by:
I am walking through some of the very first sample code from the book "Beginning .NET Game Programming" from Apress. I identify his sample code with //SC This code puzzles me: Graphics graph...
14
by: Pmb | last post by:
At the moment I'm using Borland's C++ (http://www.borland.com/products/downloads/download_cbuilder.html#) I want to be able to take an array of points and plot them on the screen. Is there a way...
5
by: Charles A. Lackman | last post by:
Hello, I have created a complete PrintDocument and need to create an image from it. How is this done? e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.HighQuality...
6
by: Chris Dunaway | last post by:
The method for printing documents in .Net can be confusing, especially for newer users. I would like to create a way to simplify this process. My idea would be implemented using a PrintDocument...
12
by: Xah Lee | last post by:
Of Interest: Introduction to 3D Graphics Programing http://xahlee.org/3d/index.html Currently, this introduction introduces you to the graphics format of Mathematica, and two Java Applet...
6
by: active | last post by:
I have an image and a graphics object created (FromImage) from that image. I need to create a new image and create a new graphics object from the new image. I want the new graphics object have...
8
by: Abhiraj Chauhan | last post by:
I need someone to make an example of how to create a graphics window in VB.net 2008. I understand the basics of how to draw a rectangle and lines etc. What I need is an example of how to make a...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.