简体   繁体   English

使用ASP.NET将div捕获为图像-C#

[英]Capture div as image using asp.net - c#

In my c# windows application i successfully captured panel1 as image (bmp, jpg, png) using below code 在我的C#Windows应用程序中,我使用以下代码成功将panel1捕获为图像(bmp,jpg,png)

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. 我在ASP网站中使用此代码来捕获ASP面板作为图像。 But SystemInformation and DrawToBitmap not working in web project. 但是SystemInformationDrawToBitmap在Web项目中不起作用。 Can anyone give me an idea that how to capture asp panel with actual width and height. 谁能给我一个想法,如何捕获具有实际宽度和高度的asp面板。

my full windows app button code is given below 我的完整Windows应用程序按钮代码如下

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;
        }

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#)? 首先获取控件的呈现html输出,如下所示: 如何获取.NET(C#)中UserControl的HTML输出?

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 然后如图所示, 将HTML字符串转换为图像将html转换为图像

 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 您必须将HTMLrenderer库添加到您的项目中,才能正常工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM