Click here to Skip to main content
15,902,112 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have written this program with "using System.Drawing;":

I do not know if it worked because there is no yellow just a blank webpage.

Is there something I need to put in Default.aspx so this will work?

C#
Bitmap xPanel = new Bitmap(200, 200);
Graphics objGraphicPanel = Graphics.FromImage(xPanel);
//Background White
SolidBrush yellowBrush = new SolidBrush(Color.Yellow);
objGraphicPanel.FillRectangle(yellowBrush, 0, 0, 200, 200);

//Text
SolidBrush blackBrush = new SolidBrush(Color.Black);
Font horzFont = new Font("Verdana", 10, FontStyle.Regular);
objGraphicPanel.DrawString("Test", horzFont, blackBrush, 15, 80);

//Line
Pen colorPen = new Pen(Color.Green, 2);
objGraphicPanel.DrawLine(colorPen, 10, 100, 120, 190);

xPanel.Save("c:\path\graphicline.png");
objGraphicPanel.Dispose();
xPanel.Dispose();


What I have tried:

xPanel.Save(Server.MapPath("graphicline.png"), System.Drawing.Imaging.ImageFormat.Png);
Posted
Updated 3-Jun-16 20:16pm

1 solution

The only change I needed to make to your code was to add an atsign:
C#
xPanel.Save(@"c:\path\graphicline.png");
Without it your code would not compile. Changing that to a valid folder on my PC, I can then look at the file it generated: Dropbox - graphicline.png[^] As you can see, it's yellow, has the word "Text" and a green diagonal line.
But...that won't display on a webpage. Not unless you specifically tell the page to display it, which you don't!
Instead of saving the image to a file and disposing of the bitmap - which isn't a good idea in a website, as multiple users may want different pictures - You need to display the bitmap directly.
There are a lot of ways to do that, but the simplest is to add an Image control to your page and give it the bytes that make up the image:
ASP.NET
<img ID="MyImage" runat="server" />

C#
using (Bitmap xPanel = new Bitmap(200, 200))
    {
    using (Graphics objGraphicPanel = Graphics.FromImage(xPanel))
        {
        //Background White
        SolidBrush yellowBrush = new SolidBrush(Color.Yellow);
        objGraphicPanel.FillRectangle(yellowBrush, 0, 0, 200, 200);
        //Text
        SolidBrush blackBrush = new SolidBrush(Color.Black);
        Font horzFont = new Font("Verdana", 10, FontStyle.Regular);
        objGraphicPanel.DrawString("Test", horzFont, blackBrush, 15, 80);
        //Line
        Pen colorPen = new Pen(Color.Green, 2);
        objGraphicPanel.DrawLine(colorPen, 10, 100, 120, 190);
        MemoryStream ms = new MemoryStream();
        xPanel.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
        string base64 = Convert.ToBase64String(ms.ToArray());
        MyImage.Src = "data:image/gif;base64," + base64;
        }
    }
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900