0

In my c# windows application i successfully captured panel1 as image (bmp, jpg, png) using below code

int x1 = SystemInformation.WorkingArea.X;
int y1 = SystemInformation.WorkingArea.Y;
int width1 = panel1.Width;
int height1 = panel1.Height;
Rectangle bounds1 = new Rectangle(x1, y1, width1, height1);
Bitmap img1 = new Bitmap(width1, height1);
panel1.DrawToBitmap(img1, bounds1);

I used this code in my asp website to capture asp panel as image. But SystemInformation and DrawToBitmap not working in web project. Can anyone give me an idea that how to capture asp panel with actual width and height.

my full windows app button code is given below

private void savetypebtn_Click(object sender, EventArgs e)
    {
        try
        {
            int x1 = SystemInformation.WorkingArea.X;
            int y1 = SystemInformation.WorkingArea.Y;
            int width1 = panel1.Width;
            int height1 = panel1.Height;
            Rectangle bounds1 = new Rectangle(x1, y1, width1, height1);
            Bitmap img1 = new Bitmap(width1, height1);
            panel1.DrawToBitmap(img1, bounds1);

            string saved_file = "";
            savedialog.InitialDirectory = "D:";
            savedialog.Title = "Save Quotation";
            savedialog.FileName = "";
            savedialog.Filter = "Jpg image|*.jpg|Bitmap image|*.bmp|png image|*.png";

            if (savedialog.ShowDialog() != DialogResult.Cancel)
            {
                saved_file = savedialog.FileName;
                img1.Save(saved_file, System.Drawing.Imaging.ImageFormat.Bmp);
            }
        }
        catch (Exception ex)
        {
            errorlbl.Visible=True;
            errorlbl.Text = ex.Message;
        }
Paolo Forgia
  • 6,572
  • 8
  • 46
  • 58
Narender Godara
  • 105
  • 2
  • 10

1 Answers1

0

This is the solution I found. First get the rendered html output of a control as shown here How do I get the HTML output of a UserControl in .NET (C#)?

StringBuilder myStringBuilder = new StringBuilder();
TextWriter myTextWriter = new StringWriter(myStringBuilder);
HtmlTextWriter myWriter = new HtmlTextWriter(myTextWriter);
Panel1.RenderControl(myWriter);
string html = myTextWriter.ToString();   

Then as shown here Convert HTML string to image convert the html into an image

 public void ConvertHtmlToImage()
{

  System.Drawing.Image img = HtmlRender.RenderToImage(html, new Size(400, 200), Color.Linen);//html is the varialbe obtained above
   img.Save(Server.MapPath("/Images/save.jpg"), ImageFormat.Jpeg);
}

You have to add HTMLrenderer library to your project for this to work

Community
  • 1
  • 1
Sujit.Warrier
  • 2,815
  • 2
  • 28
  • 47