Hỏi đáp

Chia sẻ kiến thức, cùng nhau phát triển

In., Lưu một panel/nhiều picturebox + label/textbox

8 năm trước 667 lượt xem 6 bình luận 8 năm trước

Cho mình hỏi là mình muốn lưu và in một panel hoặc một nhóm các object như picturebox, label, textbox,... từ màn hình ra thì mình cần phải thêm những gì vào lệnh inạ?

Còn lệnh lưu thì phải code như thế nào để có thể lưu được một nùi đó thành một tệp ảnh (jpeg, png,...).

Mình cảm ơn ạ!

Bình luận

Để bình luận, bạn cần đăng nhập bằng tài khoản Howkteam.

Đăng nhập
Accodius đã bình luận 8 năm trước

PrintDialog myPrintDialog = new PrintDialog();
System.Drawing.Bitmap memoryImage = new System.Drawing.Bitmap(panel1.Width, panel1.Height);
panel1.DrawToBitmap(memoryImage, panel1.ClientRectangle);
if (myPrintDialog.ShowDialog() == DialogResult.OK)
{
System.Drawing.Printing.PrinterSettings values;
values = myPrintDialog.PrinterSettings;
myPrintDialog.Document = printDocument1;
printDocument1.PrintController = new StandardPrintController();
printDocument1.Print();
}
printDocument1.Dispose();
 //declare event handler for printing in constructor
        printdoc1.PrintPage += new PrintPageEventHandler(printdoc1_PrintPage);

    //Rest of the code
    Bitmap MemoryImage;
    public void GetPrintArea(Panel pnl)
    {
        MemoryImage = new Bitmap(pnl.Width, pnl.Height);
        pnl.DrawToBitmap(MemoryImage, new Rectangle(0, 0, pnl.Width, pnl.Height));
    }
    protected override void OnPaint(PaintEventArgs e)
    {
        if (MemoryImage != null)
        {
            e.Graphics.DrawImage(MemoryImage, 0, 0);
            base.OnPaint(e);
        }
    }
    void printdoc1_PrintPage(object sender, PrintPageEventArgs e)
    {
        Rectangle pagearea = e.PageBounds;
        e.Graphics.DrawImage(MemoryImage, (pagearea.Width / 2) - (this.panel1.Width / 2), this.panel1.Location.Y);
    }
    public void Print(Panel pnl)
    {
        pannel = pnl;
        GetPrintArea(pnl);
        previewdlg.Document = printdoc1;
        previewdlg.ShowDialog();
    }
    private void button1_Click(object sender, EventArgs e)
    {
        Print(this.panel1);
    }

Link :

https://social.msdn.microsoft.com/Forums/vstudio/en-US/872012ba-347f-4bb6-af1f-157b4025ee95/how-to-print-the-panel-and-its-content?forum=csharpgeneral

https://stackoverflow.com/questions/10605840/print-panel-in-windows-form-c-sharp

cái dưới có vẻ code khoa học hơn, bạn có thể đến tận trang để đọc xem thử, mình chỉ tìm giúp thôi, được thì tick cho mình nha :v

Tạ Ngọc Minh đã bình luận 8 năm trước

Thế cái Lưu panel thì làm kiểu gì bạn ơi!!!!

Câu hỏi mới nhất