This article will show you how you can save a panel as image
with control inside the panel as image in windows application using c#.net and
vb.net.
Some of my previous articles are as follows: Dynamically
Change Form Background Color C#.Net in Windows Application, Dynamically
Add Textbox Control in Panel and Clear All on Single Click Using C#.Net in
Windows Application, Confirmation
Message on Button Click and Capture YES/NO Click by User in Windows
application, C#, Error
Handling in Windows Application Using C#.Net, Restrict
User to Enter Only Negative, Non-Negative and Decimal Number in TextBox Using
C#.Net in Windows Application.
So for this article first we will create a new windows
application and in this application we will add a panel control and some other
control inside the panel control.
Now we will create a folder into our bin -> debug folder.
Now we add the below code on button click event.
C#.Net:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public
Form1()
{
InitializeComponent();
}
private
void button2_Click(object
sender, EventArgs e)
{
try
{
using
(var bmp = new
Bitmap(panel1.Width, panel1.Height))
{
panel1.DrawToBitmap(bmp, new Rectangle(0,
0, bmp.Width, bmp.Height));
bmp.Save("Image/PanelImage.png");
}
MessageBox.Show("Image saved successfully.");
}
catch
{
MessageBox.Show("Error.....");
}
}
}
}
|
Imports System.Drawing
Imports System.Windows.Forms
Namespace WindowsFormsApplication1
Partial Public Class Form1
Inherits
Form
Public
Sub New()
InitializeComponent()
End Sub
Private
Sub button2_Click(ByVal
sender As Object,
ByVal e As
EventArgs)
Try
Using
bmp = New Bitmap(panel1.Width, panel1.Height)
panel1.DrawToBitmap(bmp, New Rectangle(0, 0, bmp.Width, bmp.Height))
bmp.Save("Image/PanelImage.png")
End
Using
MessageBox.Show("Image saved successfully.")
Catch
MessageBox.Show("Error.....")
End
Try
End Sub
End Class
End Namespace
|
In above code I have converted the panel into an image and saved it into the image folder. Herer check the output.
Now click on save button on success full save you will get success message. On error you will get error message.
Now check the image folder.
DOWNLOAD
Hi. How are you? I just wanted to inform that I can't can't download the examples. Could you please solve it? :) Thanks :)
ReplyDeletePlease try to download again
Delete