Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create Graphics object from User Control

Status
Not open for further replies.

brucegonewild

Programmer
Jan 25, 2008
22
CA
Hi there,
I have a form made up of two web-user-controls. In one web-user control, I have a third party graph (saved as an image). In the other user-control I have a data-grid displaying very simple set of rows with some text and numbers in it.

What I have to do is to export the content of my main form (the container of the two user-controls) to PDF and JPEG. The third party can save itself to a JPEG file no problem. when I try to attach the content of my summary table as binary to the file containing the saved-chart, the entire file gets corrupted and un-viewable.


Aftter hours of trying I have concluded the best thing to do would be to use the "Graphics" library and turn the data-grid to an image. My problem of course is creating a graphics object for this summary-table.

My question:
Do you know of any way to create a "Graphics" object from a Web-User-Control. It seems like System.Windows.Forms.Control has a "CreateGraphic()" method whereas System.Web.UI.Control doesn't...

Your help is much appreciated!
 
you want to take the formatting and data of the gridview and convert this to an image. System.Web.UI.Control is part of the asp.net framework. it's responsible for converting objects into html. nothing more, nothing less.

creating an image from the data is not a responsibility of the web. this should be considered a service of the domain. I don't have any experience with creating images. that said i would approach the problem like this:

1. fetch data
2. convert to xml (serialize)
3. transform xml to formatted table use xslt
4. send the results of step 3 to an image/bitmap object.
5. save.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Thanks for the reply.

Yes that would be a very logical approach. My problem is really combining my Chart-Image and the data-image.

So let's say both images are created. How would I create a final image of the two? My problem is really the lack of experience with the Graphics library, as well. It's less of the logic and more of a "What .net syntax to use" kinda problem.

If someone could provide me with some code-examples that'd be great. Just so that you know I'm not being lazy, here's the code I have so far:

Code:
        //temp file:
        string newFile = Path.GetTempFileName();
        //File-info to open a write-stream to this temporary file:
        FileInfo fi = new FileInfo(newFile);
        //use a binary writer to save chart-imag in this temp file:
        BinaryWriter bw = new BinaryWriter(fi.OpenWrite());
        //save chart image into this temporary file:
        bw.Write(this.BinaryReport.GetBuffer());
        //we're done with this binary writer... close it:
        bw.Close();

        //a bitmap to restore our chart-image:
        Bitmap TmpBitmap = new Bitmap(newFile);
        //attach a graphics to this chart-image so we can write on it:
        Graphics TmpGraphics = Graphics.FromImage(TmpBitmap);

          
        //add summary table text to this chart-image: (as you can imagine 
       //the text is displayed on top of my chart. 
       //I need the text kinda attached to the end of the image)
        TmpGraphics.DrawString(this.GetSummaryTableBinary(), 
                                new Font("Arial", 12.0f, GraphicsUnit.Pixel), 
                                new SolidBrush(Color.Red), 0, 0);


        newFile = Path.GetTempFileName();
        TmpBitmap.Save(newFile, ImageFormat.Jpeg);
        TmpBitmap.Dispose();

        Response.AddHeader("content-disposition", "attachment; filename=" + newFile);
        this.Response.ContentType = this.GetMimeType("." + this.SaveAsFormat.ToString());
        this.Response.BinaryWrite(File.ReadAllBytes(newFile));
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top