C# 滚动截图帮助类
2022/2/25 17:21:27
本文主要是介绍C# 滚动截图帮助类,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TeeChartTest
{
class ControlImage
{
#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>
/// 获取图形
/// </summary>
/// <param name="MyControl"></param>
/// <returns></returns>
public static Bitmap GetImage(Control MyControl)
{
Point ScrollMaxInfo = GetScrollPoint(MyControl);
Size ControlMaxSize = GetControlSize(MyControl, ScrollMaxInfo);
Bitmap ControlImage = new Bitmap(ControlMaxSize.Width+100, ControlMaxSize.Height+100);
Graphics MainGraphics = Graphics.FromImage(ControlImage);
Bitmap TempImage = new Bitmap(MyControl.Width - 16, MyControl.Height - 16);
int StepWidth = GetScrollNumb(MyControl.Width - 16, ScrollMaxInfo.X);
int StepHeight = GetScrollNumb(MyControl.Height - 16, ScrollMaxInfo.Y);
int LogWidth = 0;
int LogHeigh = 0;
MoveBar(0, 0, MyControl);
for (int i = 0; i != StepWidth; i++)
{
MoveBar(1, 0, MyControl);
LogHeigh = 0;
for (int z = 0; z != StepHeight; z++)
{
MyControl.DrawToBitmap(TempImage, new Rectangle(0, 0, MyControl.Width - 16, MyControl.Height - 16));
MainGraphics.DrawImage(TempImage, LogWidth, LogHeigh);
MoveBar(1, MyControl.Height - 16 + LogHeigh, MyControl);
LogHeigh = GetScrollPoint(1, MyControl);
}
MoveBar(0, MyControl.Width - 16 + LogWidth, MyControl);
LogWidth = GetScrollPoint(0, MyControl);
}
MainGraphics.Dispose();
TempImage.Dispose();
return ControlImage;
}
/// <summary>
/// 获取目前滚动条位置
/// </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>
/// 获取循环几次获得图形
/// </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>
/// 获得控件不需要滚动条的宽和高(最终图形大小)
/// </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>
/// 获取滚动条数据
/// </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>
/// 移动控件滚动条位置
/// </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);
}
}
}
}
这篇关于C# 滚动截图帮助类的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2022-03-01沐雪多租宝商城源码从.NetCore3.1升级到.Net6的步骤
- 2024-12-06使用Microsoft.Extensions.AI在.NET中生成嵌入向量
- 2024-11-18微软研究:RAG系统的四个层次提升理解与回答能力
- 2024-11-15C#中怎么从PEM格式的证书中提取公钥?-icode9专业技术文章分享
- 2024-11-14云架构设计——如何用diagrams.net绘制专业的AWS架构图?
- 2024-05-08首个适配Visual Studio平台的国产智能编程助手CodeGeeX正式上线!C#程序员必备效率神器!
- 2024-03-30C#设计模式之十六迭代器模式(Iterator Pattern)【行为型】
- 2024-03-29c# datetime tryparse
- 2024-02-21list find index c#
- 2024-01-24convert toint32 c#