Save panel as picture in Winfrom

Source: Internet
Author: User

private void Button1_Click (object sender, EventArgs e)
{
Point scrollmaxinfo = Getscrollpoint (Panel1);
Size controlmaxsize = getcontrolsize (Panel1, scrollmaxinfo);

Bitmap controlimage = new Bitmap (controlmaxsize.width, controlmaxsize.height);
Graphics maingraphics = Graphics.fromimage (controlimage);
Bitmap tempimage = new Bitmap (Panel1. Width-16, Panel1. HEIGHT-16);

int stepwidth = Getscrollnumb (Panel1. Width-16, scrollmaxinfo.x);
int stepheight = Getscrollnumb (Panel1. Height-16, SCROLLMAXINFO.Y);

int logwidth = 0;
int logheigh = 0;
Movebar (0, 0, Panel1);

for (int i = 0; I! = Stepwidth; i++)
{
Movebar (1, 0, Panel1);
Logheigh = 0;
for (int z = 0; Z! = stepheight; z++)
{
Panel1. DrawToBitmap (Tempimage, New Rectangle (0, 0, Panel1. Width-16, Panel1. HEIGHT-16));
Maingraphics.drawimage (Tempimage, Logwidth, Logheigh);
Movebar (1, Panel1. Height-16 + Logheigh, panel1);
Logheigh = Getscrollpoint (1, Panel1);
}
Movebar (0, Panel1. Width-16 + logwidth, panel1);
Logwidth = Getscrollpoint (0, Panel1);
}
Maingraphics.dispose ();
Tempimage.dispose ();
Controlimage.save (System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "123.png");
}

#region API
[StructLayout (layoutkind.sequential, CharSet = CharSet.Auto)]
public struct SCROLLINFO
{
public UINT cbsize;
public UINT Fmask;
public int nMin;
public int nMax;
public UINT Npage;
public int nPos;
public int ntrackpos;
}
public enum Scrollbarinfoflags
{
Sif_range = 0x0001,
Sif_page = 0x0002,
Sif_pos = 0x0004,
Sif_disablenoscroll = 0x0008,
Sif_trackpos = 0x0010,
Sif_all = (Sif_range | Sif_page | Sif_pos | Sif_trackpos)
}
public enum Scrollbarrequests
{
Sb_lineup = 0,
Sb_lineleft = 0,
Sb_linedown = 1,
Sb_lineright = 1,
Sb_pageup = 2,
Sb_pageleft = 2,
Sb_pagedown = 3,
Sb_pageright = 3,
Sb_thumbposition = 4,
Sb_thumbtrack = 5,
Sb_top = 6,
Sb_left = 6,
Sb_bottom = 7,
Sb_right = 7,
Sb_endscroll = 8
}
[DllImport ("user32.dll", CharSet = CharSet.Auto)]
public static extern int Getscrollinfo (INTPTR hwnd, int bar, ref SCROLLINFO SI);
[DllImport ("user32")]
public static extern int SetScrollPos (IntPtr hWnd, int nBar, int nPos, bool Rush);
[DllImport ("user32.dll", CharSet = CharSet.Auto)]
public static extern int SendMessage (IntPtr hWnd, int msg, int wParam, int lParam);
#endregion

<summary>
Get current scroll bar position
</summary>
<param name= "Bar" ></param>
<param name= "MyControl" ></param>
<returns></returns>
public static int Getscrollpoint (int Bar, Control mycontrol)
{
SCROLLINFO SCROLLINFO = new SCROLLINFO ();
Scrollinfo.cbsize = (UINT) System.Runtime.InteropServices.Marshal.SizeOf (ScrollInfo);
Scrollinfo.fmask = (UINT) Scrollbarinfoflags.sif_all;

Getscrollinfo (Mycontrol.handle, Bar, ref ScrollInfo);

return scrollinfo.npos;
}
<summary>
Get the loop several times to get the graph
</summary>
<returns></returns>
private static int Getscrollnumb (int mycontrolnumb, int scrollmaxinfonumb)
{
Double Step = (double) scrollmaxinfonumb/(double) mycontrolnumb;
step++;

if ((int) Step = = Step)
{
return (int) Step;
}
Else
{
return (int) Step + 1;
}
}
<summary>
Get control does not need scroll bar width and height (final graphics size)
</summary>
<param name= "MyControl" ></param>
<param name= "Scrollmaxinfo" ></param>
<returns></returns>
private static Size Getcontrolsize (Control mycontrol, point Scrollmaxinfo)
{
return new Size (MyControl.Size.Width + scrollmaxinfo.x-16, MyControl.Size.Height + scrollmaxinfo.y-16);
}
<summary>
Get scroll bar data
</summary>
<param name= "MyControl" ></param>
<param name= "Scrollsize" ></param>
<returns></returns>
private static Point Getscrollpoint (Control mycontrol)
{
Point maxscroll = new Point ();

SCROLLINFO SCROLLINFO = new SCROLLINFO ();
Scrollinfo.cbsize = (UINT) System.Runtime.InteropServices.Marshal.SizeOf (ScrollInfo);
Scrollinfo.fmask = (UINT) Scrollbarinfoflags.sif_all;

Getscrollinfo (Mycontrol.handle, 1, ref ScrollInfo);
MAXSCROLL.Y = Scrollinfo.nmax-(int) scrollinfo.npage;
if ((int) Scrollinfo.npage = = 0) maxscroll.y = 0;
Getscrollinfo (mycontrol.handle, 0, ref ScrollInfo);
maxscroll.x = Scrollinfo.nmax-(int) scrollinfo.npage;
if ((int) Scrollinfo.npage = = 0) maxscroll.x = 0;
return maxscroll;
}
<summary>
Move control scroll bar position
</summary>
<param name= "Bar" ></param>
<param name= "Point" ></param>
<param name= "MyControl" ></param>
private static void Movebar (int Bar, int point, Control mycontrol)
{
if (Bar = = 0)
{
SetScrollPos (mycontrol.handle, 0, point, true);
SendMessage (Mycontrol.handle, (int) 0x0114, (int) scrollbarrequests.sb_thumbposition, 0);
}
Else
{
SetScrollPos (Mycontrol.handle, 1, point, true);
SendMessage (Mycontrol.handle, (int) 0x0115, (int) scrollbarrequests.sb_thumbposition, 0);
}
}

Save panel as picture in Winfrom

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.