473,723 Members | 2,218 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 PicDocumentPrin ter : PrintDocument
{
private Image _bmpOfPage = null;
// When the page is printed.
protected override void OnPrintPage(Pri ntPageEventArgs e)
{
// Draw an ellipse on printer Graphic so we have something
to see.
e.Graphics.Draw Ellipse(new Pen(Color.Blue, 5), 0, 0, 100,
100);

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

IntPtr hdcPrint = e.Graphics.GetH dc();
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.ReleaseH dc(hdcBmp);
e.Graphics.Rele aseHdc(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("GDI 32.DLL", 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 3503
You might get more response if you post to
microsoft.publi c.dotnet.framew ork.drawing.

Robin S.
---------------------------------------
<ic*******@gmai l.comwrote in message
news:11******** *************@s 48g2000cws.goog legroups.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 PicDocumentPrin ter : PrintDocument
{
private Image _bmpOfPage = null;
// When the page is printed.
protected override void OnPrintPage(Pri ntPageEventArgs e)
{
// Draw an ellipse on printer Graphic so we have something
to see.
e.Graphics.Draw Ellipse(new Pen(Color.Blue, 5), 0, 0, 100,
100);

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

IntPtr hdcPrint = e.Graphics.GetH dc();
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.ReleaseH dc(hdcBmp);
e.Graphics.Rele aseHdc(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("GDI 32.DLL", 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
2178
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
8442
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 bitmap in array and then draw it on PictureBox or Form but fast... Classic methode bitmap.SetPixel is very slow... Please help me. Thanks.
6
2826
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 or image to make invisible. here is code for mouseevent: Protected Overrides Sub OnMouseDown(ByVal e As System.Windows.Forms.MouseEventArgs) Me.PegPicking = True
2
6778
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 Paint or Photoshop I would like to do this programmatically. My code below attempts to load in the original bitmap, crop it at the desired location, and save it to the correct file. I don't think I'm doing this right, I tried messing with different...
4
6868
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 I can quite looking. Thank you Kalvin
7
11627
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 is proving to be more difficult. These pictureboxes are bound to an AccessDB. If the user wants to add an image, they select an image using an OpenFileDialog: Dim result As DialogResult = Pic_Sel.ShowDialog() If (result = DialogResult.OK) Then
27
3384
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 gdata will work if I do: gdata = this->get(lp); But that is not what I want to do I get some cryptic errors when I write the code like that is commented out. 49 C:\Documents and Settings\Work\My Documents\C++\Dungeon Adventure2\gaphic.cpp...
4
1792
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 together. With the code below, i only see the graphic, nothing else (no table, label, button). How can i see the table and below the table, the graphic?
8
10175
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 however shows white as the PictureBox moves but I want it to be transparent. The PictureBox BackColor is set to Transparent .. but no affect. I have used PhotoShop to make the image background transparent .. again no affect. If I make the image a...
0
8868
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9241
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8062
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6685
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5996
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4764
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3209
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2612
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2149
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.