如何将带有数据的用户控件转换并保存到图像中?

Posted

技术标签:

【中文标题】如何将带有数据的用户控件转换并保存到图像中?【英文标题】:How to convert and save User Control with data in to image? 【发布时间】:2022-01-12 06:24:21 【问题描述】:

我希望用户能够将自定义用户控件保存到图像中,而数据来自其他表单。 我似乎找不到将其与数据转换为图像的方法。

这里是用户控制

这是显示用户控件的方法...位图部分是当我尝试捕获用户控件的图像但它没有数据...

 void AddIncomingmsd(String Name, String ID, String Date, String Department, String Sched, String Pos, 
        String basic, String Holiday, String overtime, String Leave, String tbasic, String tHoliday, String tovertime, 
        String tLeave, String tincome, String sss, String phic, String pib, String tax, String cash, String deduct, String net, string date, string company)
    
        var payslip = new PaySlip1();
        pnlcontainer.Controls.Add(payslip);
        payslip.BringToFront();
        payslip.Dock = DockStyle.Top;
        payslip.Padding = new Padding(0, 10, 0, 0);

        b = new Bitmap(payslip.Width, payslip.Height);
        payslip.DrawToBitmap(b, new Rectangle(0, 0, b.Width, b.Height));


        payslip.Empname = Name;
        payslip.Empid = ID;
        payslip.payperiod = Date;
        payslip.depart = Department;
        payslip.sched = Sched;
        payslip.position = Pos;

        payslip.nname = Name;
        payslip.iid = ID;
        payslip.payp = Date;
        payslip.date = date;

        payslip.basic = basic;
        payslip.holiday = Holiday;
        payslip.overtime = overtime;
        payslip.leave = Leave;

        payslip.tbasic = tbasic;
        payslip.tholiday = tHoliday;
        payslip.tovertime = tovertime;
        payslip.tleave = tLeave;
        payslip.tincome = tincome;

        payslip.SSS = sss;
        payslip.PHIC = phic;
        payslip.PIB = pib;
        payslip.tax = tax;
        payslip.Cashadv = cash;
        payslip.tDeduct = deduct;

        payslip.net = net;
        payslip.company = company;

    

这是向用户控件插入数据的方法...

 public void nameidgross()
    
                string name = Computation.name;
                string ID = Computation.id;
                string payperio = Payroll.payperiod;
                string depart = Computation.depart;
                string sched = Computation.sched;
                string position = Computation.position;
                double rate = Computation.rate;
                double holirate = Computation.holidayrate;
                double overhr = Computation.overhr;
                double leavehr = Computation.leavehr;
                double basic = Math.Round(Computation.basic, 2);
                double holipay = Computation.holidaypay;
                double tover = Computation.totalover;
                double tleave = Computation.totalleave;
                double tgross = Computation.tlgross;
                double cash = Computation.totalcash;
                double tdeduc = Computation.totaldeduct;
                double net = Math.Round(Computation.netpay, 2);
                string company = PayrollForm.company;
  
                    ss = Computation.ss;
                    ph = Computation.php;
                    pg = Computation.pg;

  AddIncomingmsd(name, ID, payperio, depart, sched,
                    position, rate.ToString(), holirate.ToString(),
                    overhr.ToString(), leavehr.ToString(),
                    basic.ToString(), holipay.ToString(), tover.ToString(),
                    tleave.ToString(), tgross.ToString(), ss.ToString(),
                    ph.ToString(), pg.ToString(), Computation.Ttax.ToString(), cash.ToString(),
                    deducted.ToString(), net.ToString(), date, company);
 

而且这个方法是为按钮保存图片...

 private void button1_Click(object sender, EventArgs e)
    
        nameidgross();
        SaveFileDialog saveFileDialog1 = new SaveFileDialog();
        saveFileDialog1.FileName = "image " + Payroll.payperiod + ".jpg";
        if (saveFileDialog1.ShowDialog() == DialogResult.OK)
        
            using (var Stream = saveFileDialog1.OpenFile())
            
                b.Save(Stream, ImageFormat.Bmp);
            
        
    

【问题讨论】:

Control.DrawToBitmap(Bitmap, Rectangle) 方法。 【参考方案1】:

您应该能够使用Control.DrawToBitmap(Bitmap, Rect) 根据表单中的内容创建位图。像这样的流操作通常会引发异常,因此在 try/catch 块中执行这些操作是最佳实践。然后,您可以像这样将位图保存到文件(如果需要):

try

    using (var bmp = new Bitmap(width, height))
    
        ActiveForm.DrawToBitmap(bmp, targetRectSize);
        bmp.Save(@"c:/directory/filetobecreated.imgextension");
    

catch

    <handle exception...>

有关详细信息,请参阅 Control.DrawToBitmap() 的 MSDN 文档。

【讨论】:

以上是关于如何将带有数据的用户控件转换并保存到图像中?的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 Javascript 将 div 转换为图像

如何将上传的图像保存到 MongoDB 集合并检索它

如何在节点中使用 multer 将图像上传到 cloudinary 并与其他数据一起表达

Firebase Firestore iOS:如何将图像保存到 Firestore 中的集合中?

在从 UIActivityController 拒绝保存图像的权限后,如何将用户重定向到设置?

如何将数据保存到 mongodb 中的子文档中?我正在将农场子文档转换为 null