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的更多相关文章
随机推荐
- Win7 VS2013环境编译boost1_58_0
备忘,发现好多不常用的东西不记笔记再想用要重新花时间找,所以试着开始记笔记,写入博客吧. 首先去官网下最新的版本 http://www.boost.org/ 写本文时boost最新版本为1_58_0, ...
- CString成员函数详解[转]
1.构造函数(常用) CString( const unsigned char* psz ); 例:char s[]="abcdef"; cha ...
- c# richTextBox1添加内容并将滚动条滚动到当前焦点处
1. StringBuilder sb = new StringBuilder(); StringBuilder的改变比string快多了 2. sb.Append("\r\n" ...
- iframe父子元素获取
jquery.js调用iframe父窗口与子窗口元素的方法 1. jquery在iframe子页面获取父页面元素代码如下: $("#objid",parent.document) ...
- mybatis 时间区间比较
直接上代码,此时数据库使用的Date类型: <if test="minCreateTime != null and minCreateTime != ''"> < ...
- 02:PostgreSQL Character Sets
在利用postGIS导入shapefile文件到postgresql数据库的时候,老是提示字符串的问题,或者是乱码,试了好几种都不行,于是度娘之.... 使用默认的UTF8,提示信息是:建议使用LAT ...
- 从晶体管到web浏览器
几年前写了这个,想放到网上,招聘很困难,很多现代计算机科学教育真的很糟,很难找到懂得现代计算机堆栈的第一(基本)原理的人. 如果我曾经再次得到了12周的自由,我会提供这个作为课余学习的play,我也想 ...
- lua rc4算法实现
由于项目需要,用python django写restful接口遇到瓶颈,python django+uwsgi处理请求是会阻塞的, 如果阻塞请求不及时处理,会卡住越来越多的其它的请求,导致越来越多的5 ...
- (概率 01背包) Just another Robbery -- LightOJ -- 1079
http://lightoj.com/volume_showproblem.php?problem=1079 Just another Robbery As Harry Potter series i ...
- java基础-day26
第03天 java基础加强 今日内容介绍 u BeanUtils概述及使用 u XML简介及约束 u XML解析 第1章 XML简介 1.1 XML基本语法 1.1.1 XML概述 XML全称为 ...