この記事では、QQ スタイルのスクリーンショット機能を実装するための C# のサンプル コードを主に紹介します。編集者がそれを参考として共有します。エディターに従って見てみましょう。この関数は 2 つの部分で構成されています。最初の部分はフォーム コードであり、もう 1 つの部分は補助メソッドです。参考のためにコードを直接投稿してください:
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; } } #endregion #region 点击button2按钮事件 private void button2_Click(object sender, EventArgs e) { this.label1.Text = "裁剪后的图片宽度:"+this.pictureBox2.Width.ToString(); this.label2.Text = "裁剪后的图片高度:"+this.pictureBox2.Height.ToString(); } #endregion #region 缩放、裁剪图像用到的变量 /// <summary> /// /// </summary> int x1; //鼠标按下时横坐标 int y1; //鼠标按下时纵坐标 int width; //所打开的图像的宽 int heigth; //所打开的图像的高 bool HeadImageBool = false; // 此布尔变量用来判断pictureBox1控件是否有图片 #endregion #region 画矩形使用到的变量 Point p1; //定义鼠标按下时的坐标点 Point p2; //定义移动鼠标时的坐标点 Point p3; //定义松开鼠标时的坐标点 #endregion #region 鼠标按下时发生的事件 private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { this.Cursor = Cursors.Cross; this.p1 = new Point(e.X, e.Y); x1 = e.X; y1 = e.Y; if (this.pictureBox1.Image != null) { HeadImageBool = true; } else { HeadImageBool = false; } } #endregion #region 移动鼠标发生的事件 private void pictureBox1_MouseMove(object sender, MouseEventArgs e) { if (this.Cursor == Cursors.Cross) { this.p2 = new Point(e.X, e.Y); if ((p2.X - p1.X) > 0 && (p2.Y - p1.Y) > 0) //当鼠标从左上角向开始移动时P3坐标 { this.p3 = new Point(p1.X, p1.Y); } if ((p2.X - p1.X) < 0 && (p2.Y - p1.Y) > 0) //当鼠标从右上角向左下方向开始移动时P3坐标 { this.p3 = new Point(p2.X, p1.Y); } if ((p2.X - p1.X) > 0 && (p2.Y - p1.Y) < 0) //当鼠标从左下角向上开始移动时P3坐标 { this.p3 = new Point(p1.X, p2.Y); } if ((p2.X - p1.X) < 0 && (p2.Y - p1.Y) < 0) //当鼠标从右下角向左方向上开始移动时P3坐标 { this.p3 = new Point(p2.X, p2.Y); } this.pictureBox1.Invalidate(); //使控件的整个图面无效,并导致重绘控件 } } #endregion #region 松开鼠标发生的事件,实例化ImageCut1类对像 ImageCut1 IC1; //定义所画矩形的图像对像 private void pictureBox1_MouseUp(object sender, MouseEventArgs e) { if (HeadImageBool) { width = this.pictureBox1.Image.Width; heigth = this.pictureBox1.Image.Height; if ((e.X - x1) > 0 && (e.Y - y1) > 0) //当鼠标从左上角向右下方向开始移动时发生 { IC1 = new ImageCut1(x1, y1, Math.Abs(e.X - x1), Math.Abs(e.Y - y1)); //实例化ImageCut1类 } if ((e.X - x1) < 0 && (e.Y - y1) > 0) //当鼠标从右上角向左下方向开始移动时发生 { IC1 = new ImageCut1(e.X, y1, Math.Abs(e.X - x1), Math.Abs(e.Y - y1)); //实例化ImageCut1类 } if ((e.X - x1) > 0 && (e.Y - y1) < 0) //当鼠标从左下角向右上方向开始移动时发生 { IC1 = new ImageCut1(x1, e.Y, Math.Abs(e.X - x1), Math.Abs(e.Y - y1)); //实例化ImageCut1类 } if ((e.X - x1) < 0 && (e.Y - y1) < 0) //当鼠标从右下角向左上方向开始移动时发生 { IC1 = new ImageCut1(e.X, e.Y, Math.Abs(e.X - x1), Math.Abs(e.Y - y1)); //实例化ImageCut1类 } this.pictureBox2.Width = (IC1.KiCut1((Bitmap)(this.pictureBox1.Image))).Width; this.pictureBox2.Height = (IC1.KiCut1((Bitmap)(this.pictureBox1.Image))).Height; this.pictureBox2.Image = IC1.KiCut1((Bitmap)(this.pictureBox1.Image)); this.Cursor = Cursors.Default; } else { this.Cursor = Cursors.Default; } } #endregion #region 获取所选矩形图像 /// <summary> /// /// </summary> /// <param name="Width"></param> /// <param name="Height"></param> /// <returns></returns> public Image GetSelectImage1(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; } } #endregion #region 重新绘制pictureBox1控件,即移动鼠标画矩形 private void pictureBox1_Paint(object sender, PaintEventArgs e) { if (HeadImageBool) { Pen p = new Pen(Color.Black, 1);//画笔 p.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash; //Bitmap bitmap = new Bitmap(strHeadImagePath); Bitmap bitmap = Bi; Rectangle rect = new Rectangle(p3, new Size(System.Math.Abs(p2.X - p1.X), System.Math.Abs(p2.Y - p1.Y))); e.Graphics.DrawRectangle(p, rect); } else { } } #endregion } }
2 番目の部分は補助メソッド クラスです
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; namespace ImageClassLib { public class ImageCut1 { #region 剪裁图片方法 /// <summary> /// 剪裁 -- 用GDI+ /// </summary> /// <param name="b">原始Bitmap,即需要裁剪的图片</param> /// <param name="StartX">开始坐标X</param> /// <param name="StartY">开始坐标Y</param> /// <param name="iWidth">宽度</param> /// <param name="iHeight">高度</param> /// <returns>剪裁后的Bitmap</returns> public Bitmap KiCut1(Bitmap b) { if (b == null) { return null; } int w = b.Width; int h = b.Height; if (X >= w || Y >= h) { return null; } if (X + Width > w) { Width = w - X; } if (Y + Height > h) { Height = h - Y; } try { Bitmap bmpOut = new Bitmap(Width, Height, PixelFormat.Format24bppRgb); Graphics g = Graphics.FromImage(bmpOut); // Create rectangle for displaying image. Rectangle destRect = new Rectangle(0, 0, Width, Height); //所画的矩形正确,它指定所绘制图像的位置和大小。 将图像进行缩放以适合该矩形。 // Create rectangle for source image. Rectangle srcRect = new Rectangle(X, Y, Width, Height); //srcRect参数指定要绘制的 image 对象的矩形部分。 将此部分进行缩放以适合 destRect 参数所指定的矩形。 g.DrawImage(b, destRect, srcRect, GraphicsUnit.Pixel); //resultG.DrawImage(initImage, new System.Drawing.Rectangle(0, 0, side, side), new System.Drawing.Rectangle(0, 0, initWidth, initHeight), System.Drawing.GraphicsUnit.Pixel); g.Dispose(); return bmpOut; } catch { return null; } } #endregion #region ImageCut1类的构造函数 public int X; public int Y; public int Width ; public int Height; /// <summary> /// ImageCut1类的构造函数,ImageCut1类用来获取鼠标在pictureBox1控件所画矩形内的图像 /// </summary> /// <param name="x表示鼠标在pictureBox1控件上按下时的横坐标"></param> /// <param name="y表示鼠标在pictureBox1控件上按下时的纵坐标"></param> /// <param name="width表示鼠标在pictureBox1控件上松开鼠标的宽度"></param> /// <param name="heigth表示鼠标在pictureBox1控件上松开鼠标的高度"></param> public ImageCut1(int x, int y, int width, int heigth) { X = x; Y = y; Width = width; Height = heigth; } #endregion } }
達成される効果は次のとおりです:
上記はのスクリーンショットです。 QQ スタイルの C# 実装 関数のサンプル コードが詳しく紹介されています。関連コンテンツについては、PHP 中国語 Web サイト (www.php.cn) を参照してください。

如何使用C#编写时间序列预测算法时间序列预测是一种通过分析过去的数据来预测未来数据趋势的方法。它在很多领域,如金融、销售和天气预报中有广泛的应用。在本文中,我们将介绍如何使用C#编写时间序列预测算法,并附上具体的代码示例。数据准备在进行时间序列预测之前,首先需要准备好数据。一般来说,时间序列数据应该具有足够的长度,并且是按照时间顺序排列的。你可以从数据库或者

如何使用Redis和C#开发分布式事务功能引言分布式系统的开发中,事务处理是一项非常重要的功能。事务处理能够保证在分布式系统中的一系列操作要么全部成功,要么全部回滚。Redis是一种高性能的键值存储数据库,而C#是一种广泛应用于开发分布式系统的编程语言。本文将介绍如何使用Redis和C#来实现分布式事务功能,并提供具体代码示例。I.Redis事务Redis

如何实现C#中的人脸识别算法人脸识别算法是计算机视觉领域中的一个重要研究方向,它可以用于识别和验证人脸,广泛应用于安全监控、人脸支付、人脸解锁等领域。在本文中,我们将介绍如何使用C#来实现人脸识别算法,并提供具体的代码示例。实现人脸识别算法的第一步是获取图像数据。在C#中,我们可以使用EmguCV库(OpenCV的C#封装)来处理图像。首先,我们需要在项目

C#开发中如何处理跨域请求和安全性问题在现代的网络应用开发中,跨域请求和安全性问题是开发人员经常面临的挑战。为了提供更好的用户体验和功能,应用程序经常需要与其他域或服务器进行交互。然而,浏览器的同源策略导致了这些跨域请求被阻止,因此需要采取一些措施来处理跨域请求。同时,为了保证数据的安全性,开发人员还需要考虑一些安全性问题。本文将探讨C#开发中如何处理跨域请

Redis在C#开发中的应用:如何实现高效的缓存更新引言:在Web开发中,缓存是提高系统性能的常用手段之一。而Redis作为一款高性能的Key-Value存储系统,能够提供快速的缓存操作,为我们的应用带来了不少便利。本文将介绍如何在C#开发中使用Redis,实现高效的缓存更新。Redis的安装与配置在开始之前,我们需要先安装Redis并进行相应的配置。你可以

如何使用C#编写动态规划算法摘要:动态规划是求解最优化问题的一种常用算法,适用于多种场景。本文将介绍如何使用C#编写动态规划算法,并提供具体的代码示例。一、什么是动态规划算法动态规划(DynamicProgramming,简称DP)是一种用来求解具有重叠子问题和最优子结构性质的问题的算法思想。动态规划将问题分解成若干个子问题来求解,通过记录每个子问题的解,

如何在C#中实现遗传算法引言:遗传算法是一种模拟自然选择和基因遗传机制的优化算法,其主要思想是通过模拟生物进化的过程来搜索最优解。在计算机科学领域,遗传算法被广泛应用于优化问题的解决,例如机器学习、参数优化、组合优化等。本文将介绍如何在C#中实现遗传算法,并提供具体的代码示例。一、遗传算法的基本原理遗传算法通过使用编码表示解空间中的候选解,并利用选择、交叉和

如何使用C#编写背包问题算法背包问题(KnapsackProblem)是一个经典的组合优化问题,它描述了一个给定容量的背包以及一系列物品,每个物品都有自己的价值和重量。目标是找到一种最佳策略,使得在不超过背包容量的情况下,装入背包的物品总价值最大。在C#中,可以通过动态规划方法来解决背包问题。具体实现如下:usingSystem;namespace


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

AI Hentai Generator
AIヘンタイを無料で生成します。

人気の記事

ホットツール

WebStorm Mac版
便利なJavaScript開発ツール

mPDF
mPDF は、UTF-8 でエンコードされた HTML から PDF ファイルを生成できる PHP ライブラリです。オリジナルの作者である Ian Back は、Web サイトから「オンザフライ」で PDF ファイルを出力し、さまざまな言語を処理するために mPDF を作成しました。 HTML2FPDF などのオリジナルのスクリプトよりも遅く、Unicode フォントを使用すると生成されるファイルが大きくなりますが、CSS スタイルなどをサポートし、多くの機能強化が施されています。 RTL (アラビア語とヘブライ語) や CJK (中国語、日本語、韓国語) を含むほぼすべての言語をサポートします。ネストされたブロックレベル要素 (P、DIV など) をサポートします。

ドリームウィーバー CS6
ビジュアル Web 開発ツール

PhpStorm Mac バージョン
最新(2018.2.1)のプロフェッショナル向けPHP統合開発ツール

MinGW - Minimalist GNU for Windows
このプロジェクトは osdn.net/projects/mingw に移行中です。引き続きそこでフォローしていただけます。 MinGW: GNU Compiler Collection (GCC) のネイティブ Windows ポートであり、ネイティブ Windows アプリケーションを構築するための自由に配布可能なインポート ライブラリとヘッダー ファイルであり、C99 機能をサポートする MSVC ランタイムの拡張機能が含まれています。すべての MinGW ソフトウェアは 64 ビット Windows プラットフォームで実行できます。

ホットトピック



