using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
class PicCompress
{
public byte[] GetPictureData(string imagepath)
{
FileStream file = new FileStream(imagepath, FileMode.Open);
byte[] by = new byte[file.Length];
file.Read(by, 0, by.Length);
file.Close();
return by;
}
public byte[] GetPictureData(Image imgPhoto)
{
//将Image转换成流数据,并保存为byte[]
MemoryStream mstream = new MemoryStream();
imgPhoto.Save(mstream, System.Drawing.Imaging.ImageFormat.Bmp);
byte[] byData = new Byte[mstream.Length];
mstream.Position = 0;
mstream.Read(byData, 0, byData.Length);
mstream.Close();
return byData;
}
/// <summary>
/// 无损压缩图片
/// </summary>
/// <param name="sFile">原图片</param>
/// <param name="dFile">压缩后保存位置</param>
/// <param name="dHeight">高度</param>
/// <param name="dWidth">宽度</param>
/// <param name="flag">压缩质量 1-100</param>
/// <returns></returns>
public bool CompressImage(Image iSource, string dFile, int dHeight, int dWidth, int flag)
{
if (File.Exists(dFile))
{
File.Delete(dFile);
}
ImageFormat tFormat = iSource.RawFormat;
int sW = 0, sH = 0;
//按比例缩放
Size tem_size = new Size(iSource.Width, iSource.Height);
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;
}
Bitmap ob = new Bitmap(sW, sH);
Graphics g = Graphics.FromImage(ob);
g.Clear(Color.WhiteSmoke);
g.CompositingQuality = CompositingQuality.HighQuality;
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(iSource, new Rectangle(0, 0, sW, sH), 0, 0, iSource.Width, iSource.Height, GraphicsUnit.Pixel);
g.Dispose();
//以下代码为保存图片时,设置压缩质量
EncoderParameters ep = new EncoderParameters();
long[] qy = new long[1];
qy[0] = flag;//设置压缩的比例1-100
EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy);
ep.Param[0] = eParam;
try
{
ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo jpegICIinfo = null;
for (int x = 0; x < arrayICI.Length; x++)
{
if (arrayICI[x].FormatDescription.Equals("JPEG"))
{
jpegICIinfo = arrayICI[x];
break;
}
}
if (jpegICIinfo != null)
{
ob.Save(dFile, jpegICIinfo, ep);//dFile是压缩后的新路径
}
else
{
ob.Save(dFile, tFormat);
}
return true;
}
catch
{
return false;
}
finally
{
iSource.Dispose();
ob.Dispose();
}
}
/// <summary>
/// 有遮挡截屏
/// </summary>
/// <param name="fc"></param>
/// <returns></returns>
public Image CaptureScreen(Form fc)
{
Image MyImage = new Bitmap(fc.Width, fc.Height);
Graphics g = Graphics.FromImage(MyImage);
g.CopyFromScreen(fc.Location.X, fc.Location.Y, 0, 0, fc.Size);
return MyImage;
}
/// <summary>
/// 无遮挡截屏
/// </summary>
public Image CaptureScreen2(Form fc)
{
Image MyImage = null;
IntPtr hdc = GetWindowDC(fc.Handle);
LPPOINT lphook = new LPPOINT();
lphook.x = 0;
lphook.y = 0;
ClientToScreen(fc.Handle, ref lphook);
ScreenToClient(fc.Handle, ref lphook);
Rectangle rect = new Rectangle(lphook.x, lphook.y, fc.Width, fc.Height);
if (hdc != IntPtr.Zero)
{
IntPtr hdcMem = CreateCompatibleDC(hdc);
if (hdcMem != IntPtr.Zero)
{
IntPtr hbitmap = CreateCompatibleBitmap(hdc, fc.Width, fc.Height);
if (hbitmap != IntPtr.Zero)
{
IntPtr ip = SelectObject(hdcMem, hbitmap);
if (ip != IntPtr.Zero)
{
bool a = PrintWindow(fc.Handle, hdcMem, 0);
DeleteObject(hbitmap);
Image tempImg = Image.FromHbitmap(hbitmap);
Bitmap b = new Bitmap(tempImg);
MyImage = b.Clone(rect, b.PixelFormat);
}
}
}
}
return MyImage;
}
[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
private static extern bool BitBlt(
IntPtr hdcDest, // 目标 DC的句柄
int nXDest,
int nYDest,
int nWidth,
int nHeight,
IntPtr hdcSrc, // 源DC的句柄
int nXSrc,
int nYSrc,
System.Int32 dwRop // 光栅的处理数值
);
[System.Runtime.InteropServices.DllImportAttribute("User32.dll")]
private static extern IntPtr GetWindowDC(
IntPtr hwnd
);
[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
private static extern IntPtr CreateCompatibleDC(
IntPtr hdc
);
[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
private static extern IntPtr CreateCompatibleBitmap(
IntPtr hdc,
int nWidth, // width of bitmap, in pixels
int nHeight // height of bitmap, in pixels
);
[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
private static extern IntPtr SelectObject(
IntPtr hdc,// handle to DC
IntPtr hgdiobj // handle to object
);
[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
private static extern bool DeleteObject(
IntPtr hObject // handle to graphic object
);
[System.Runtime.InteropServices.DllImportAttribute("User32.dll")]
private static extern bool PrintWindow(
IntPtr hwnd, // Window to copy
IntPtr hdcBlt, // HDC to print into
int nFlags // Optional flags
);
[System.Runtime.InteropServices.DllImportAttribute("User32.dll")]
private static extern int ReleaseDC(
IntPtr hWnd, // handle to window
IntPtr hDC // handle to DC
);
[System.Runtime.InteropServices.DllImportAttribute("User32.dll")]
private static extern bool ScreenToClient(
IntPtr hWnd, // handle to window
ref LPPOINT lpPoint // screen coordinates
);
[System.Runtime.InteropServices.DllImportAttribute("User32.dll")]
private static extern bool ClientToScreen(
IntPtr hWnd, // handle to window
ref LPPOINT lpPoint // screen coordinates
);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct LPPOINT
{
public int x;
public int y;
}
}
调用方法:
PicCompress pc = new PicCompress();
//无遮挡截屏
Image memory = pc.CaptureScreen(this);
//有遮挡截屏
Image memory = pc.CaptureScreen2(this);
//改路径
string path = FilePath_PIC + "ABC.jpeg";
//图像压缩
pc.CompressImage(memory, path, this.Height, this.Width, 80);
memory.Dispose();
GC.Collect();