C# 实现实现QQ式截图功能实例代码式截图功能实例代码
本篇文章主要介绍了C# 实现QQ式截图功能实例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
这个功能一共有两部分组成,第一部分是窗体代码,另外的一部分是一个辅助方法。直接贴出代码,以供大家参考:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using ImageClassLib;
namespace ImageShear
{
public partial class Demo: Form
{
public Demo()
{
InitializeComponent();
}
#region 点击打开图像
public string strHeadImagePath; //打开图片的路径
Bitmap Bi; //定义位图对像
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "*.gif|*.jpg|*.JPEG|*.JPEG|*.bmp|*.bmp"; //设置读取图片类型
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
strHeadImagePath = openFileDialog1.FileName;
//this.Show(strHeadImagePath);
Bi = new Bitmap(strHeadImagePath); //使用打开的图片路径创建位图对像
ImageCut1 IC = new ImageCut1(40, 112, this.pictureBox1.Width, this.pictureBox1.Height); //实例化ImageCut类,四个参数据分别表示为:x、y、width、heigth,(40、112)表示pictureBox1的Lcation的坐标,(120、144)表示pictureBox1控件的宽度和高度
this.pictureBox1.Image = IC.KiCut1((Bitmap)(this.GetSelectImage(this.pictureBox1.Width, this.pictureBox1.Height))); //(120、144)表示pictureBox1控件的宽度和高度
//this.pictureBox1.Image = (this.GetSelectImage(120, 144));
}
catch (Exception ex)
{
MessageBox.Show("格式不对");
ex.ToString();
}
}
}
#endregion
#region 定义显示图像方法,即将打开的图像在pictureBox1控件显示
public void Show(string strHeadImagePath)
{
this.pictureBox1.Load(@strHeadImagePath); //
}
#endregion
#region 获取图像
/// <summary>
/// 获取指定宽度和高度的图像即使图片和pictureBox1控件一样宽和高,返回值为图片Image
/// </summary>
/// <param name="Width表示宽"></param>
/// <param name="Height表示高"></param>
/// <returns></returns>
public Image GetSelectImage(int Width, int Height)
{
//Image initImage = this.pictureBox1.Image;
Image initImage = Bi;
//原图宽高均小于模版,不作处理,直接保存
if (initImage.Width <= Width && initImage.Height <= Height)
{
//initImage.Save(fileSaveUrl, System.Drawing.Imaging.ImageFormat.Jpeg);
return initImage;
}
else
{
//原始图片的宽、高
int initWidth = initImage.Width;
int initHeight = initImage.Height;
//非正方型先裁剪为正方型
if (initWidth != initHeight)
{
//截图对象
System.Drawing.Image pickedImage = null;
System.Drawing.Graphics pickedG = null;
//宽大于高的横图
if (initWidth > initHeight)
{
//对象实例化
pickedImage = new System.Drawing.Bitmap(initHeight, initHeight);
pickedG = System.Drawing.Graphics.FromImage(pickedImage);
//设置质量
pickedG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
pickedG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//定位
Rectangle fromR = new Rectangle((initWidth - initHeight) / 2, 0, initHeight, initHeight);
Rectangle toR = new Rectangle(0, 0, initHeight, initHeight);
//画图
pickedG.DrawImage(initImage, toR, fromR, System.Drawing.GraphicsUnit.Pixel);
//重置宽
initWidth = initHeight;
}
//高大于宽的竖图
else
{
//对象实例化
pickedImage = new System.Drawing.Bitmap(initWidth, initWidth);
pickedG = System.Drawing.Graphics.FromImage(pickedImage);
//设置质量
pickedG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
pickedG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//定位
Rectangle fromR = new Rectangle(0, (initHeight - initWidth) / 2, initWidth, initWidth);
Rectangle toR = new Rectangle(0, 0, initWidth, initWidth);
//画图
pickedG.DrawImage(initImage, toR, fromR, System.Drawing.GraphicsUnit.Pixel);
//重置高
initHeight = initWidth;
}
initImage = (System.Drawing.Image)pickedImage.Clone();
// //释放截图资源
pickedG.Dispose();
pickedImage.Dispose();
}
return initImage;