ScreenOper
/// <summary>
/// 屏幕操作类
/// Add by 2017-07-25
/// 1、屏幕生成Image 方法
/// 2、Image按百分比压缩 方法
/// 3、Image根据指定像素生成方法
/// </summary>
public class ScreenOper
{
[DllImport("user32.dll")]
static extern bool GetCursorInfo(out CURSORINFO pci); private const Int32 CURSOR_SHOWING = 0x00000001;
[StructLayout(LayoutKind.Sequential)]
struct POINT
{
public Int32 x;
public Int32 y;
} [StructLayout(LayoutKind.Sequential)]
struct CURSORINFO
{
public Int32 cbSize;
public Int32 flags;
public IntPtr hCursor;
public POINT ptScreenPos;
}
/// <summary>
/// 屏幕截图方法
/// 1、将整个屏幕截图
/// 2、并画出鼠标形状
/// </summary>
/// <returns></returns>
public static Image ScreenToImg()
{
Image myimage = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
Graphics g = Graphics.FromImage(myimage);
g.CopyFromScreen(new Point(, ), new Point(, ), new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height));
CURSORINFO pci;
pci.cbSize = Marshal.SizeOf(typeof(CURSORINFO));
GetCursorInfo(out pci);
System.Windows.Forms.Cursor cur = new System.Windows.Forms.Cursor(pci.hCursor);
cur.Draw(g, new Rectangle(pci.ptScreenPos.x - , pci.ptScreenPos.y - , cur.Size.Width, cur.Size.Height));
return myimage;
} /// <summary>
/// 将图片按百分比压缩,flag取值1到100,越小压缩比越大
/// </summary>
/// <param name="iSource"></param>
/// <param name="outPath"></param>
/// <param name="flag"></param>
/// <returns></returns>
public static bool ImgZip(Image iSource, string outPath, int flag)
{
ImageFormat tFormat = iSource.RawFormat;
EncoderParameters ep = new EncoderParameters();
long[] qy = new long[];
qy[] = flag;
EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy);
ep.Param[] = eParam;
try
{
ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageDecoders();
ImageCodecInfo jpegICIinfo = null; jpegICIinfo = arrayICI.Where(c => c.FormatDescription.Equals("JPEG")).FirstOrDefault(); if (jpegICIinfo != null)
iSource.Save(outPath, jpegICIinfo, ep);
else
iSource.Save(outPath, tFormat);
}
catch
{
return false;
}
iSource.Dispose();
return true;
} /// <summary>
/// 将图片按百分比压缩,flag取值1到100,越小压缩比越大
/// </summary>
/// <param name="iSource">原图片</param>
/// <param name="flag">1-100</param>
/// <returns></returns>
public static Image ImgZip(Image iSource, int flag)
{
Image img;
MemoryStream mStream = new MemoryStream();
ImageFormat tFormat = iSource.RawFormat;
EncoderParameters ep = new EncoderParameters();
long[] qy = new long[];
qy[] = flag;
EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy);
ep.Param[] = eParam;
try
{
ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageDecoders();
ImageCodecInfo jpegICIinfo = null;
jpegICIinfo = arrayICI.Where(c => c.FormatDescription.Equals("JPEG")).FirstOrDefault();
if (jpegICIinfo != null)
iSource.Save(mStream, jpegICIinfo, ep);
img = Image.FromStream(mStream);
mStream.Close();
}
catch
{
return iSource;
}
iSource.Dispose();
return img;
}
/// <summary>
/// 将图片根据像素生成
/// </summary>
/// <param name="srcImage"></param>
/// <returns></returns>
public static Image SetImageSize(Image srcImage, int dWidth, int dHeight)
{
Image ob;
try
{
//按比例缩放
Size tem_size = new Size(srcImage.Width, srcImage.Height);
int sW = , sH = ;
if (tem_size.Width > dHeight || tem_size.Width > dWidth)
{
if ((tem_size.Width * dHeight) > (tem_size.Height * dWidth))
{
sW = dWidth;
sH = (dWidth * tem_size.Height) / tem_size.Width;
}
else
{
sH = dHeight;
sW = (tem_size.Width * dHeight) / tem_size.Height;
}
}
else
{
sW = tem_size.Width;
sH = tem_size.Height;
} ob = new Bitmap(dWidth, dHeight);
Graphics g = Graphics.FromImage(ob);
g.Clear(Color.WhiteSmoke);
g.CompositingQuality = CompositingQuality.HighQuality;
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(srcImage, new Rectangle((dWidth - sW) / , (dHeight - sH) / , sW, sH), , , srcImage.Width, srcImage.Height, GraphicsUnit.Pixel);
g.Dispose();
}
catch (Exception)
{
return null;
}
return ob;
}
}
ScreenOper的更多相关文章
随机推荐
- c# devexpress学习绘图
用字典方式存储数据并绘图:http://www.xuebuyuan.com/465384.html 数据库存储数据,并对图形作各种设置:http://www.cnblogs.com/xuhaibiao ...
- svn 提交代码 自动过滤技巧
操作 在用svn管理版本时,有时希望在提交到服务器时,能过滤掉指定后缀名的所有文件,或指定名称的文件夹.文件名. 常见的文件夹名称和文件名如下: bin obj debug temppe *.suo ...
- java 冒泡排序 二分查找 选择排序 插入排序
下面这个程序是先定义一个整型数组,然后将其中的元素反序赋值,再用冒泡排序进行排序以后用二分查找来查找其中是否有某个数,返回值为-1时表示这个数可能小于这个数组的最小值或大小这个数组的最大值,-2表示这 ...
- js 匿名函数 用法
JS执行顺序为从上到下 先声明存储匿名函数的变量放在JS文件中 <script src="/Scripts/niming.js" type="text/javasc ...
- 2.1.4synchronized方法与锁对象
为了证明线程锁的是对象 测试 package com.cky.bean; /** * Created by chenkaiyang on 2017/12/4. */ public class MyOb ...
- (区间dp 或 记忆化搜素 )Brackets -- POJ -- 2955
http://poj.org/problem?id=2955 Description We give the following inductive definition of a “regular ...
- POJ2229 Sumsets
Sumsets Time Limit: 2000MS Memory Limit: 200000K Total Submissions: 19024 Accepted: 7431 Descrip ...
- hdu 1716 排列
题目 这道题是全排列问题,主要注意的是格式问题.觉得下面这种写法最为巧妙 #include <cstdio> #include <iostream> #include < ...
- hdu 4135 [a,b]中n互质数个数+容斥
http://acm.hdu.edu.cn/showproblem.php?pid=4135 给定一个数n,求某个区间[a,b]内有多少数与这个数互质. 对于一个给定的区间,我们如果能够求出这个区间内 ...
- delphi 窗体自适应屏幕分辨率
delphi 窗体自适应屏幕分辨率 这是个困惑我很长时间的问题,到今天终于得到解决了. 话说Delphi有个很强的窗体设计器,这一点让VC粉丝垂涎三尺而不可得.但是,Delphi里设计的窗体并没有自动 ...