473,396 Members | 1,982 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,396 software developers and data experts.

How do you copy a Graphic to a Bitmap Image?

On an academic note, I want to copy a Graphic to an Image (Bitmap).
I have the Graphic object but not the origin image from which it
originates; this is because I'm overriding the PrintDocument class
OnPagePrint method which only provides me an e.Graphic (see code
sample below).

The FCL doesn't seem to natively support Graphic =(Image)Bitmap. I
have tried BitBlt workaround in the source code to copy printer
e.Graphic into the bmp Graphic, but the bitmap ends up being blank
even thought the printer Graphic is not.
Any thoughts as to a resolution?

using System;
using System.Windows.Forms;

using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Printing;

namespace RemotePrinting
{
class PicDocumentPrinter : PrintDocument
{
private Image _bmpOfPage = null;
// When the page is printed.
protected override void OnPrintPage(PrintPageEventArgs e)
{
// Draw an ellipse on printer Graphic so we have something
to see.
e.Graphics.DrawEllipse(new Pen(Color.Blue, 5), 0, 0, 100,
100);

// Create bitmap Image.
Bitmap bmp = new Bitmap(500,500, e.Graphics);
Graphics gfxBmp = Graphics.FromImage(bmp);

IntPtr hdcPrint = e.Graphics.GetHdc();
IntPtr hdcBmp = gfxBmp.GetHdc();

/* Try to copy printer e.Graphic onto Bitmap Graphic.
* The bitmap will be blank -- why did the ellipse not
copy?
* The printer shows the ellipse.
*/
BitBlt(hdcBmp, 0, 0, 500, 500, hdcPrint, 0, 0, SRCCOPY);

gfxBmp.ReleaseHdc(hdcBmp);
e.Graphics.ReleaseHdc(hdcPrint);
e.HasMorePages = false;

// This bmp is blank, but should not be.
_bmpOfPage = bmp;
}

public Image Result
{
get { return _bmpOfPage; }
}

int SRCCOPY = 13369376;

[System.Runtime.InteropServices.DllImport("GDI32.DL L", CharSet
= System.Runtime.InteropServices.CharSet.Auto)]
private static extern bool BitBlt(IntPtr hdcDest, int nXDest,
int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int
nYSrc, Int32 dwRop);

}
}

Mar 4 '07 #1
1 3448
You might get more response if you post to
microsoft.public.dotnet.framework.drawing.

Robin S.
---------------------------------------
<ic*******@gmail.comwrote in message
news:11*********************@s48g2000cws.googlegro ups.com...
On an academic note, I want to copy a Graphic to an Image (Bitmap).
I have the Graphic object but not the origin image from which it
originates; this is because I'm overriding the PrintDocument class
OnPagePrint method which only provides me an e.Graphic (see code
sample below).

The FCL doesn't seem to natively support Graphic =(Image)Bitmap. I
have tried BitBlt workaround in the source code to copy printer
e.Graphic into the bmp Graphic, but the bitmap ends up being blank
even thought the printer Graphic is not.
Any thoughts as to a resolution?

using System;
using System.Windows.Forms;

using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Printing;

namespace RemotePrinting
{
class PicDocumentPrinter : PrintDocument
{
private Image _bmpOfPage = null;
// When the page is printed.
protected override void OnPrintPage(PrintPageEventArgs e)
{
// Draw an ellipse on printer Graphic so we have something
to see.
e.Graphics.DrawEllipse(new Pen(Color.Blue, 5), 0, 0, 100,
100);

// Create bitmap Image.
Bitmap bmp = new Bitmap(500,500, e.Graphics);
Graphics gfxBmp = Graphics.FromImage(bmp);

IntPtr hdcPrint = e.Graphics.GetHdc();
IntPtr hdcBmp = gfxBmp.GetHdc();

/* Try to copy printer e.Graphic onto Bitmap Graphic.
* The bitmap will be blank -- why did the ellipse not
copy?
* The printer shows the ellipse.
*/
BitBlt(hdcBmp, 0, 0, 500, 500, hdcPrint, 0, 0, SRCCOPY);

gfxBmp.ReleaseHdc(hdcBmp);
e.Graphics.ReleaseHdc(hdcPrint);
e.HasMorePages = false;

// This bmp is blank, but should not be.
_bmpOfPage = bmp;
}

public Image Result
{
get { return _bmpOfPage; }
}

int SRCCOPY = 13369376;

[System.Runtime.InteropServices.DllImport("GDI32.DL L", CharSet
= System.Runtime.InteropServices.CharSet.Auto)]
private static extern bool BitBlt(IntPtr hdcDest, int nXDest,
int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int
nYSrc, Int32 dwRop);

}
}

Mar 4 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Anonieko Ramos | last post by:
> I have a graphics images that I want to convert to > ASCII art. How do I do it? > Code: - Default.aspx.cs
2
by: predseda | last post by:
I would like to ask you, if you know about something like Tanner Helland's graphic tutorials (http://tannerhelland.tripod.com/VBGraphicsTutorial.htm)but VB.Net compatibile. I need to create...
6
by: Supra | last post by:
i got board working using graphic window in vb.net but no controls adding to form. i am doing checker board game. when i clicked and moved the peg to another location(grid). but how do i get bitmap...
2
by: Mad Scientist Jr | last post by:
I have a bitmap (32 pixels high, 8192 pixels wide) that contains 255 images, each 32 pixels wide, that I would like to chop up into individual 32x32 bitmap files. Rather than spending hours in...
4
by: Kalvin | last post by:
I am trying to create an image from a graphics object. I would like to create the image and use in the application without having to save to disk. If this isn't possible, please just tell me so...
7
by: lgbjr | last post by:
Hello All, I¡¯m using a context menu associated with some pictureboxes to provide copy/paste functionality. Copying the image to the clipboard was easy. But pasting an image from the clipboard...
27
by: JoeC | last post by:
I am still working on my game and my program is getting better. Most of what I want to works. I think I am having trouble with copy constructor. Basically I want it to copy the gdata array. My...
4
by: André | last post by:
Hi, I try to create and render a graphic among other objects like e.g. tables, labels, buttons. The tables are not a problem, neither the graphic. My problem is that i can't render both...
8
by: Brian Ward | last post by:
I am looking for a simple way to set the image transparency in a PictureBox. I have a moving PictureBox containing a graphic image .. moving by incrementing its Left property. The background...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.