/// <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的更多相关文章

随机推荐

  1. High-radix routers

    The idea is to reduce H (hops), by adding explicit links between physically distant routers, thus re ...

  2. java.io.IOException: Can't read [\jre\lib\rt.jar]

    [proguard] java.io.IOException: Can't read [F:\e\java\jdk1.8.0_101\jre\lib\rt.jar] (Can't process cl ...

  3. 18) maven 项目结构:继承

    Project Inheritance [ɪn'herɪt(ə)ns] https://maven.apache.org/guides/introduction/introduction-to-the ...

  4. 基础总结篇之一:Activity生命周期[转]

    from:http://blog.csdn.net/liuhe688/article/details/6733407   基础总结篇之一:Activity生命周期 子曰:溫故而知新,可以為師矣.< ...

  5. hibernate增删改

    public class HibernateUtils { private static SessionFactory sessionFactory=null; static{ //获取config ...

  6. mach_absolute_time 使用

    今天看荣哥时间常用函数封装里有个不常见的函数 ,mach_absolute_time() ,经查询后感觉是个不错的函数,网上关于这个函数搜索以后简单整理来一下. 什么事Mach? 时间例程依赖于所需要 ...

  7. Beta阶段第二篇Scrum冲刺博客-Day1

    1.站立式会议 提供当天站立式会议照片一张 2.每个人的工作 (有work item 的ID),并将其记录在码云项目管理中: 昨天已完成的工作. 张晨晨:交接进组 郭琪容:明确任务并学习 吴玲:明确接 ...

  8. python的打开_记录

    1. 2.7版本,安装了2.7,可以在命令符里面输入cmd,然后输入python,进入python的工作环境. 2. 3.6版本 ,anaconda版本,在F:\qhpython里边找到python. ...

  9. Android-Java-封装

    先看一个未封装的Demo案例一: package android.java.oop03; class Person { int age; } public class PottingDemo { pu ...

  10. [leetcode 50]remove element

    1 题目 Given an array and a value, remove all instances of that value in place and return the new leng ...