Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#3.0
Hi
 
Please let me know , If you know any solution to make an image file or binary file from the image that has been drawn in the e.graphics.
 
I want to draw text using e.graphics instead of using bitmap because drawing on e.graphics has high quality and then I want to have a copy of the data on that.
 
How I can copy later the content of the saved file on e.graphics again for printing?
 
Thanks
Posted 27-Feb-13 6:58am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Here is what you can do:
 
You render your graphics by overriding System.Windows.Forms.OnPaint or handling the event System.Windows.Forms.Paint. It's simpler to show the first case:
 
public partial class MyControl { // base class is some control of Form, suppose the base class is shown in other partial part

    protected virtual void OnPaint(PaintEventArgs e) {
        // do something
    }
 
} //class MyControl
 
What is that "do something"? This is your rendering I assume you already have. All you need to do it to abstract out this rendering from particular instance of System.Drawing.Graphics. Again:
 
    protected virtual void OnPaint(PaintEventArgs e) {
        Render(e.Graphics);
    }
 
    //...

    void Render(System.Drawing.Graphics graphics) {
        // use graphics the way you did before
    }
 
Are you getting the idea? Now, the method void Render(System.Drawing.Graphics) is something you can reuse for instances of System.Drawing.Graphics of different nature. It's already used for on-screen rendering, but what else could you do with it? Right, you can call it with printing or bitmap graphics.
 
Create a bitmap first, of appropriate size; please see the first link below. To get the instance of System.Drawing.Graphics to be used for drawing on some bitmap, you use this static factory method:
http://msdn.microsoft.com/en-us/library/system.drawing.graphics.fromimage.aspx[^].
 
Now you can draw on a bitmap and save this bitmap:
http://msdn.microsoft.com/en-us/library/system.drawing.bitmap.aspx[^],
http://msdn.microsoft.com/en-us/library/ktx83wah.aspx[^],
http://msdn.microsoft.com/en-us/library/ms142147.aspx[^],
http://msdn.microsoft.com/en-us/library/9t4syfhh.aspx[^].
 
Now, a bonus: you can use the same rendering method with an instance of System.Drawing.Graphics used for printing:
http://msdn.microsoft.com/en-us/library/system.drawing.printing.printdocument.aspx[^].
 
Good luck,
—SA
  Permalink  
v2
Comments
afshinsh at 27-Feb-13 14:36pm
   
Hi Sergey
Thanks a lot for your answer, would you please send a simple project that writes a simple text on e.graphic and saves it on a file and fetch it from the file.
the reason that i can't use print document is this that when there is too many pages to print it will slow down.
I appreciate that.
Sergey Alexandrovich Kryukov at 27-Feb-13 14:51pm
   
For goodness sake, no! It would take too much time.
I have an unfinished article with complete code which cover this and a lot more. This "a lot" make my doubtful. If you kind of reluctant to do you job by yourself, I'm not sure you will agree to scan through all my code, as it's big. :-)
 
My answer is already much more detailed than a typical forum answer. You have absolutely everything you need. Please, don't be so lazy. The most effective way it so use your own project. Of course, you are welcome to ask more questions is you stuck.
 
And please accept my answer formally (green button); it has all you need and even more... thanks.
 
—SA
Sergey Alexandrovich Kryukov at 27-Feb-13 14:59pm
   
As to the PrintDocument, being slow is not an argument. Do you know how to print faster? I don't think so. You should use it, but perhaps allow to print separate parts separately. Printing should not really delay execution much. When it all is sent to printer, it does not delay anything...
—SA
afshinsh at 27-Feb-13 16:00pm
   
Thanks a lot Sergey,
The reason I asked is my lack of knowledge in c# understanding your comment, anyway I will do my best to find it out and thanks for your time.
Sergey Alexandrovich Kryukov at 27-Feb-13 16:18pm
   
Nevertheless, you are welcome to ask further question if you stuck.
Good luck, call again.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 6,803
1 Sergey Alexandrovich Kryukov 6,377
2 DamithSL 5,421
3 Manas Bhardwaj 4,841
4 Maciej Los 4,330


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 27 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100