控件布局 主要分為一個(gè)Panel + 一個(gè)PictureBox + 還有一個(gè)自定義半透明裁切平面ExtendedPanel调俘;
Form1.Designer.cs代碼如下:
namespace ImageEditer
{
partial class ImageEdit
{
///
/// 必需的設(shè)計(jì)器變量伶棒。
///
private System.ComponentModel.IContainer components = null;
///
/// 清理所有正在使用的資源。
///
/// 如果應(yīng)釋放托管資源彩库,為 true肤无;否則為 false。
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗體設(shè)計(jì)器生成的代碼
///
/// 設(shè)計(jì)器支持所需的方法 - 不要修改
/// 使用代碼編輯器修改此方法的內(nèi)容骇钦。
///
private void InitializeComponent()
{
this.splitContainer = new System.Windows.Forms.SplitContainer();
this.imageList = new System.Windows.Forms.Panel();
this.imageContainer = new System.Windows.Forms.Panel();
this.optPanel = new System.Windows.Forms.Panel();
this.reset = new System.Windows.Forms.Button();
this.rota180 = new System.Windows.Forms.Button();
this.rotaLeft = new System.Windows.Forms.Button();
this.rota90 = new System.Windows.Forms.Button();
this.roatRight = new System.Windows.Forms.Button();
this.vertical = new System.Windows.Forms.Button();
this.clip = new System.Windows.Forms.Button();
this.mirror = new System.Windows.Forms.Button();
this.ImageBox = new System.Windows.Forms.Panel();
this.pictureBox = new System.Windows.Forms.PictureBox();
this.textImport = new System.Windows.Forms.Button();
this.exportImage = new System.Windows.Forms.Button();
this.clipPanel = new ImageEditer.ExtendedPanel();
((System.ComponentModel.ISupportInitialize)(this.splitContainer)).BeginInit();
this.splitContainer.Panel1.SuspendLayout();
this.splitContainer.Panel2.SuspendLayout();
this.splitContainer.SuspendLayout();
this.imageContainer.SuspendLayout();
this.optPanel.SuspendLayout();
this.ImageBox.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
this.SuspendLayout();
//
// splitContainer
//
this.splitContainer.BackColor = System.Drawing.Color.Transparent;
this.splitContainer.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.splitContainer.Dock = System.Windows.Forms.DockStyle.Fill;
this.splitContainer.Location = new System.Drawing.Point(0, 0);
this.splitContainer.Margin = new System.Windows.Forms.Padding(0);
this.splitContainer.Name = "splitContainer";
//
// splitContainer.Panel1
//
this.splitContainer.Panel1.Controls.Add(this.imageList);
//
// splitContainer.Panel2
//
this.splitContainer.Panel2.Controls.Add(this.imageContainer);
this.splitContainer.Panel2MinSize = 100;
this.splitContainer.Size = new System.Drawing.Size(961, 480);
this.splitContainer.SplitterDistance = 236;
this.splitContainer.SplitterWidth = 6;
this.splitContainer.TabIndex = 0;
//
// imageList
//
this.imageList.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.imageList.Location = new System.Drawing.Point(0, 0);
this.imageList.Name = "imageList";
this.imageList.Size = new System.Drawing.Size(232, 476);
this.imageList.TabIndex = 1;
//
// imageContainer
//
this.imageContainer.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.imageContainer.AutoScroll = true;
this.imageContainer.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(128)))), ((int)(((byte)(128)))), ((int)(((byte)(255)))));
this.imageContainer.Controls.Add(this.optPanel);
this.imageContainer.Controls.Add(this.ImageBox);
this.imageContainer.ForeColor = System.Drawing.SystemColors.ControlText;
this.imageContainer.Location = new System.Drawing.Point(0, 0);
this.imageContainer.Name = "imageContainer";
this.imageContainer.Size = new System.Drawing.Size(717, 476);
this.imageContainer.TabIndex = 0;
//
// optPanel
//
this.optPanel.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.optPanel.BackColor = System.Drawing.Color.Gray;
this.optPanel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.optPanel.Controls.Add(this.exportImage);
this.optPanel.Controls.Add(this.textImport);
this.optPanel.Controls.Add(this.reset);
this.optPanel.Controls.Add(this.rota180);
this.optPanel.Controls.Add(this.rotaLeft);
this.optPanel.Controls.Add(this.rota90);
this.optPanel.Controls.Add(this.roatRight);
this.optPanel.Controls.Add(this.vertical);
this.optPanel.Controls.Add(this.clip);
this.optPanel.Controls.Add(this.mirror);
this.optPanel.Location = new System.Drawing.Point(0, 0);
this.optPanel.Name = "optPanel";
this.optPanel.Size = new System.Drawing.Size(718, 26);
this.optPanel.TabIndex = 2;
//
// reset
//
this.reset.Location = new System.Drawing.Point(1, 0);
this.reset.Name = "reset";
this.reset.Size = new System.Drawing.Size(60, 23);
this.reset.TabIndex = 3;
this.reset.Text = "復(fù)位";
this.reset.UseVisualStyleBackColor = true;
//
// rota180
//
this.rota180.Location = new System.Drawing.Point(412, 0);
this.rota180.Name = "rota180";
this.rota180.Size = new System.Drawing.Size(72, 23);
this.rota180.TabIndex = 9;
this.rota180.Text = "旋轉(zhuǎn)180°";
this.rota180.UseVisualStyleBackColor = true;
//
// rotaLeft
//
this.rotaLeft.Location = new System.Drawing.Point(64, 0);
this.rotaLeft.Name = "rotaLeft";
this.rotaLeft.Size = new System.Drawing.Size(60, 23);
this.rotaLeft.TabIndex = 1;
this.rotaLeft.Text = "左旋";
this.rotaLeft.UseVisualStyleBackColor = true;
//
// rota90
//
this.rota90.Location = new System.Drawing.Point(346, 0);
this.rota90.Name = "rota90";
this.rota90.Size = new System.Drawing.Size(63, 23);
this.rota90.TabIndex = 8;
this.rota90.Text = "旋轉(zhuǎn)90°";
this.rota90.UseVisualStyleBackColor = true;
//
// roatRight
//
this.roatRight.Location = new System.Drawing.Point(127, 0);
this.roatRight.Name = "roatRight";
this.roatRight.Size = new System.Drawing.Size(60, 23);
this.roatRight.TabIndex = 2;
this.roatRight.Text = "右旋";
this.roatRight.UseVisualStyleBackColor = true;
//
// vertical
//
this.vertical.Location = new System.Drawing.Point(268, 0);
this.vertical.Name = "vertical";
this.vertical.Size = new System.Drawing.Size(75, 23);
this.vertical.TabIndex = 7;
this.vertical.Text = "垂直翻轉(zhuǎn)";
this.vertical.UseVisualStyleBackColor = true;
//
// clip
//
this.clip.ForeColor = System.Drawing.SystemColors.ActiveCaptionText;
this.clip.Location = new System.Drawing.Point(487, 0);
this.clip.Name = "clip";
this.clip.Size = new System.Drawing.Size(68, 23);
this.clip.TabIndex = 4;
this.clip.Text = "裁切圖像";
this.clip.UseVisualStyleBackColor = true;
//
// mirror
//
this.mirror.Location = new System.Drawing.Point(190, 0);
this.mirror.Name = "mirror";
this.mirror.Size = new System.Drawing.Size(75, 23);
this.mirror.TabIndex = 6;
this.mirror.Text = "水平翻轉(zhuǎn)";
this.mirror.UseVisualStyleBackColor = true;
//
// ImageBox
//
this.ImageBox.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.ImageBox.AutoScroll = true;
this.ImageBox.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224)))));
this.ImageBox.Controls.Add(this.clipPanel);
this.ImageBox.Controls.Add(this.pictureBox);
this.ImageBox.Location = new System.Drawing.Point(0, 23);
this.ImageBox.Name = "ImageBox";
this.ImageBox.Size = new System.Drawing.Size(718, 455);
this.ImageBox.TabIndex = 5;
//
// pictureBox
//
this.pictureBox.BackColor = System.Drawing.Color.Transparent;
this.pictureBox.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.pictureBox.Location = new System.Drawing.Point(3, 6);
this.pictureBox.Margin = new System.Windows.Forms.Padding(0);
this.pictureBox.Name = "pictureBox";
this.pictureBox.Size = new System.Drawing.Size(32, 33);
this.pictureBox.TabIndex = 0;
this.pictureBox.TabStop = false;
//
// textImport
//
this.textImport.Location = new System.Drawing.Point(558, 0);
this.textImport.Name = "textImport";
this.textImport.Size = new System.Drawing.Size(72, 23);
this.textImport.TabIndex = 10;
this.textImport.Text = "插入文字";
this.textImport.UseVisualStyleBackColor = true;
//
// exportImage
//
this.exportImage.Location = new System.Drawing.Point(633, 0);
this.exportImage.Name = "exportImage";
this.exportImage.Size = new System.Drawing.Size(70, 23);
this.exportImage.TabIndex = 11;
this.exportImage.Text = "導(dǎo)出圖像";
this.exportImage.UseVisualStyleBackColor = true;
//
// clipPanel
//
this.clipPanel.BackColor = System.Drawing.Color.Transparent;
this.clipPanel.Location = new System.Drawing.Point(80, 9);
this.clipPanel.Name = "clipPanel";
this.clipPanel.Size = new System.Drawing.Size(27, 30);
this.clipPanel.TabIndex = 1;
//
// ImageEdit
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(961, 480);
this.Controls.Add(this.splitContainer);
this.Name = "ImageEdit";
this.Text = "圖像";
this.SizeChanged += new System.EventHandler(this.ImageEdit_ResizeBegin);
this.splitContainer.Panel1.ResumeLayout(false);
this.splitContainer.Panel2.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.splitContainer)).EndInit();
this.splitContainer.ResumeLayout(false);
this.imageContainer.ResumeLayout(false);
this.optPanel.ResumeLayout(false);
this.ImageBox.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.SplitContainer splitContainer;
private System.Windows.Forms.Panel imageContainer;
private System.Windows.Forms.Button rotaLeft;
private System.Windows.Forms.Button roatRight;
private System.Windows.Forms.Button reset;
private System.Windows.Forms.Button clip;
private System.Windows.Forms.Panel ImageBox;
private System.Windows.Forms.Panel imageList;
private System.Windows.Forms.PictureBox pictureBox;
private ExtendedPanel clipPanel;
private System.Windows.Forms.Button vertical;
private System.Windows.Forms.Button mirror;
private System.Windows.Forms.Button rota90;
private System.Windows.Forms.Button rota180;
private System.Windows.Forms.Panel optPanel;
private System.Windows.Forms.Button textImport;
private System.Windows.Forms.Button exportImage;
}
}
自定義半透明裁切平面ExtendedPanel代碼如下:
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespaceImageEditer{? ? internal classExtendedPanel : Panel? ? {? ? ? ? private const int WS_EX_TRANSPARENT = 0x20;? ? ? ? publicExtendedPanel()? ? ? ? {? ? ? ? ? ? SetStyle(ControlStyles.Opaque, true);? ? ? ? }? ? ? ? private int opacity = 50;? ? ? ? [DefaultValue(50)]? ? ? ? public intOpacity? ? ? ? {? ? ? ? ? ? get{? ? ? ? ? ? ? ? return this.opacity;? ? ? ? ? ? }? ? ? ? ? ? set{? ? ? ? ? ? ? ? if (value < 0 || value > 100)? ? ? ? ? ? ? ? ? ? throw new ArgumentException("value must be between 0 and 100");? ? ? ? ? ? ? ? this.opacity =value;? ? ? ? ? ? }? ? ? ? }? ? ? ? protected overrideCreateParams CreateParams? ? ? ? {? ? ? ? ? ? get{? ? ? ? ? ? ? ? CreateParams cp = base.CreateParams;? ? ? ? ? ? ? ? cp.ExStyle = cp.ExStyle |WS_EX_TRANSPARENT;? ? ? ? ? ? ? ? returncp;? ? ? ? ? ? }? ? ? ? }? ? ? ? protected override voidOnPaint(PaintEventArgs e)? ? ? ? {? ? ? ? ? ? using (var brush = new SolidBrush(Color.FromArgb(this.opacity * 255 / 100, this.BackColor)))? ? ? ? ? ? {? ? ? ? ? ? ? ? e.Graphics.FillRectangle(brush, this.ClientRectangle);? ? ? ? ? ? ? ? e.Graphics.DrawRectangle(new Pen(Color.Red, 2), this.ClientRectangle);? ? ? ? ? ? }? ? ? ? ? ? base.OnPaint(e);
? ? ? ? }
? ? }
}
圖片的縮放宛渐、平移操作PictureBox,圖片的旋轉(zhuǎn),鏡像主要操作?PictureBox里的Image眯搭,圖片的裁切主要通過(guò)操作自定義的ExtendedPanel大小來(lái)計(jì)算裁切后的尺寸及位置窥翩,通過(guò)轉(zhuǎn)換計(jì)算PictureBox里的Image真實(shí)裁切尺寸。
主代碼如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Window;
namespace ImageEditer
{
public partial class ImageEdit : Form
{
private static int rotaNum = 0; // 旋轉(zhuǎn)角度
private static float zoomNum = 1.0f; // 縮放比例 未使用
private static int pointRadis = 12; // 裁切點(diǎn)大小
private static Boolean clipMode = false; // 是否開啟裁切
private static Boolean textMode = false; // 是否開啟標(biāo)注
private static Pen rotaPen = new Pen(Color.Blue, 10); // 旋轉(zhuǎn)顏色 未使用
private static Pen clipPen = new Pen(Color.Red, 10); // 裁切顏色
private static string imagePath = null; // 圖像路徑
private static Image sourceImage = null; // 初始輸入圖像源
private static Image outputImage = null; // 初始輸出圖像源 未使用
private static PictureBox currentPictureBox = null; // 當(dāng)前圖像容器
private static Panel currentImageBox = null; // 當(dāng)前圖像panel
private static Panel currentClipPanel = null; // 裁切平面
private static TextBox currentTextBox = null; // 文字輸入框
private static Point one = new Point(0, 0); // 裁切調(diào)整坐標(biāo)1
private static Point two = new Point(0, 0); // 裁切調(diào)整坐標(biāo)2
private static Point three = new Point(0, 0); // 裁切調(diào)整坐標(biāo)3
private static Point four = new Point(0, 0); // 裁切調(diào)整坐標(biāo)4
private static PictureBox OneButton = new PictureBox(); // 裁切調(diào)整點(diǎn)1
private static PictureBox TwoButton = new PictureBox(); // 裁切調(diào)整點(diǎn)2
private static PictureBox ThreeButton = new PictureBox(); // 裁切調(diào)整點(diǎn)3
private static PictureBox FourButton = new PictureBox(); // 裁切調(diào)整點(diǎn)4
private static System.Drawing.Rectangle clipRect = new System.Drawing.Rectangle(0, 0, 0, 0); // 裁切矩形信息
public Point mouseDownPoint; // 記錄鼠標(biāo)點(diǎn)擊坐標(biāo)
public bool isSelected = false; // 是否點(diǎn)擊圖像
public bool isClicked = false; // 是否點(diǎn)擊裁切矩形
public bool isClickedOne = false; // 是否點(diǎn)解裁切矩形調(diào)整點(diǎn)1
public bool isClickedTwo = false; // 是否點(diǎn)解裁切矩形調(diào)整點(diǎn)2
public bool isClickedThree = false; // 是否點(diǎn)解裁切矩形調(diào)整點(diǎn)3
public bool isClickedFour = false; // 是否點(diǎn)解裁切矩形調(diào)整點(diǎn)4
public ImageEdit(){
InitializeComponent();
imagePath = @"D:\1.jpg";
this.init(this.ImageBox, this.pictureBox, imagePath);
}
/**
* 初始化組件及事件
*/
public void init(Panel currentImage, PictureBox pictureBox, string path) {
imagePath = path.Trim();
sourceImage = Image.FromFile(path);
currentImageBox = currentImage;
/************************按鈕事件**************************/
this.reset.Click += new System.EventHandler(this.reset_Click);
this.rotaLeft.Click += new System.EventHandler(this.roatleft_Click);
this.roatRight.Click += new System.EventHandler(this.roatRight_Click);
this.rota90.Click += new System.EventHandler(this.roat90_Click);
this.rota180.Click += new System.EventHandler(this.roat180_Click);
this.mirror.Click += new System.EventHandler(this.mirror_Click);
this.vertical.Click += new System.EventHandler(this.vertical_Click);
this.clip.Click += new System.EventHandler(this.clip_Click);
this.textImport.Click += new System.EventHandler(this.import_Click);
this.exportImage.Click += new System.EventHandler(this.export_Click);
currentPictureBox = pictureBox;
currentPictureBox.Location = new Point(0, 0);
currentPictureBox.Width = 0;
currentPictureBox.Height = 0;
currentPictureBox.Image = sourceImage;
currentPictureBox.Width = sourceImage.Width;
currentPictureBox.Height = sourceImage.Height;
currentPictureBox.SizeMode = PictureBoxSizeMode.Zoom;
this.SizeChanged += new EventHandler(this.ImageEdit_ResizeBegin);
currentPictureBox.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.pictureBox_MouseWheel);
currentPictureBox.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox_MouseDown);
currentPictureBox.MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBox_MouseMove);
currentPictureBox.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pictureBox_MouseUp);
/************************裁切框組件**************************/
OneButton.Size = new Size(pointRadis, pointRadis);
TwoButton.Size = new Size(pointRadis, pointRadis);
ThreeButton.Size = new Size(pointRadis, pointRadis);
FourButton.Size = new Size(pointRadis, pointRadis);
OneButton.BackColor = Color.Red;
TwoButton.BackColor = Color.Red;
ThreeButton.BackColor = Color.Red;
FourButton.BackColor = Color.Red;
OneButton.Visible = clipMode;
TwoButton.Visible = clipMode;
ThreeButton.Visible = clipMode;
FourButton.Visible = clipMode;
FourButton.TabIndex = 10000;
currentImageBox.Controls.Add(OneButton);
currentImageBox.Controls.Add(TwoButton);
currentImageBox.Controls.Add(ThreeButton);
currentImageBox.Controls.Add(FourButton);
currentImageBox.Controls.Add(pictureBox);
currentClipPanel = this.clipPanel;
currentClipPanel.BackColor = Color.Transparent;
currentClipPanel.Visible = false;
currentClipPanel.Location = new Point(0, 0);
currentClipPanel.Width = 0;
currentClipPanel.Height = 0;
currentClipPanel.MouseDown += new System.Windows.Forms.MouseEventHandler(this.clip_MouseDown);
currentClipPanel.MouseMove += new System.Windows.Forms.MouseEventHandler(this.clip_MouseMove);
currentClipPanel.MouseUp += new System.Windows.Forms.MouseEventHandler(this.clip_MouseUp);
OneButton.MouseDown += new System.Windows.Forms.MouseEventHandler(this.One_MouseDown);
OneButton.MouseMove += new System.Windows.Forms.MouseEventHandler(this.One_MouseMove);
OneButton.MouseUp += new System.Windows.Forms.MouseEventHandler(this.One_MouseUp);
TwoButton.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Two_MouseDown);
TwoButton.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Two_MouseMove);
TwoButton.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Two_MouseUp);
ThreeButton.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Three_MouseDown);
ThreeButton.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Three_MouseMove);
ThreeButton.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Three_MouseUp);
FourButton.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Four_MouseDown);
FourButton.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Four_MouseMove);
FourButton.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Four_MouseUp);
clipRect.X = 0;
clipRect.Y = 0;
clipRect.Width = currentPictureBox.Image.Width;
clipRect.Height = currentPictureBox.Image.Height;
resetImageToPanel();
}
/************************窗口事件**************************/
/**
* 窗口變化時(shí)候
*/
private void ImageEdit_ResizeBegin(object sender, EventArgs e)
{
resetImageToPanel();
}
/************************居中顯示**************************/
/**
* 縮放圖片到panel 居中
*/
private static void resetImageToPanel()
{
int windowWidth = currentImageBox.Width;
int windowHeight = currentImageBox.Height;
int imageWidth = clipRect.Width;
int imageHeight = clipRect.Height;
if (imageWidth >= imageHeight)
{
if (imageWidth > windowWidth)
{
double scaleHeight = (double)windowWidth / imageWidth * imageHeight;
currentPictureBox.Width = windowWidth;
currentPictureBox.Height = (int)scaleHeight;
}
else {
currentPictureBox.Width = imageWidth;
currentPictureBox.Height = imageHeight;
}
}
else
{
if (imageHeight > windowHeight)
{
double scaleWidth = (double)windowHeight / imageHeight * imageWidth;
currentPictureBox.Height = windowHeight;
currentPictureBox.Width = (int)scaleWidth;
}
else {
currentPictureBox.Width = imageWidth;
currentPictureBox.Height = imageHeight;
}
}
int X = (windowWidth - currentPictureBox.Width) / 2;
int Y = (windowHeight - currentPictureBox.Height) / 2;
currentPictureBox.Location = new Point(X, Y);
}
/************************按鈕事件**************************/
// 復(fù)位
private void reset_Click(object sender, EventArgs e)
{
rotaNum = 0;
resetImageToPanel();
}
// 左旋
private void roatleft_Click(object sender, EventArgs e)
{
currentPictureBox.Image = RotateImage(sourceImage, rotaNum -= 5);
}
// 右旋
private void roatRight_Click(object sender, EventArgs e)
{
currentPictureBox.Image = RotateImage(sourceImage, rotaNum += 5);
}
// 旋轉(zhuǎn)90度
private void roat90_Click(object sender, EventArgs e)
{
currentPictureBox.Image = RotateImage(sourceImage, rotaNum -= 90);
}
// 旋轉(zhuǎn)180度
private void roat180_Click(object sender, EventArgs e)
{
currentPictureBox.Image = RotateImage(sourceImage, rotaNum -= 180);
}
// 水平鏡像
private void mirror_Click(object sender, EventArgs e)
{
//currentPictureBox.Image = RotateImage(sourceImage, rotaNum);
currentPictureBox.Image = turnOverImage(currentPictureBox.Image, "mirror");
}
// 垂直鏡像
private void vertical_Click(object sender, EventArgs e)
{
//currentPictureBox.Image = RotateImage(sourceImage, rotaNum);
currentPictureBox.Image = turnOverImage(currentPictureBox.Image, "vertical");
}
// 裁切
private void clip_Click(object sender, EventArgs e)
{
clipMode = !clipMode;
if (textMode) {
textMode = false;
this.textImport.BackColor = Color.Transparent;
this.textImport.ForeColor = Color.Black;
}
if (clipMode)
{
this.clip.BackColor = Color.Blue;
this.clip.ForeColor = Color.White;
this.beginPainting();
}
else
{
this.clip.BackColor = Color.Transparent;
this.clip.ForeColor = Color.Black;
this.overPainting();
}
}
// 插入文字
private void import_Click(object sender, EventArgs e)
{
textMode = !textMode;
if (clipMode) {
this.clip.BackColor = Color.Transparent;
this.clip.ForeColor = Color.Black;
this.overPainting();
}
if (textMode)
{
this.textImport.BackColor = Color.Blue;
this.textImport.ForeColor = Color.White;
}
else
{
this.textImport.BackColor = Color.Transparent;
this.textImport.ForeColor = Color.Black;
}
}
// 導(dǎo)出圖像
private void export_Click(object sender, EventArgs e)
{
SaveFileDialog savedialog = new SaveFileDialog();
savedialog.Filter = "Png 圖片|*.png|Bmp 圖片|*.bmp|Gif 圖片|*.gif|Jpg 圖片|*.jpg|Wmf 圖片|*.wmf";
savedialog.FilterIndex = 0;
savedialog.RestoreDirectory = true; // 保存對(duì)話框是否記憶上次打開的目錄
savedialog.CheckPathExists = true; // 檢查目錄
savedialog.FileName = System.DateTime.Now.ToString("yyyyMMddHHmmss"); // 設(shè)置默認(rèn)文件名
if (savedialog.ShowDialog() == DialogResult.OK)
{
currentPictureBox.Image.Save(savedialog.FileName, System.Drawing.Imaging.ImageFormat.Png); // image為要保存的圖片
MessageBox.Show(this, "圖片保存成功鳞仙!", "信息提示");
}
}
/************************圖像鼠標(biāo)事件**************************/
private void pictureBox_MouseWheel(object sender, MouseEventArgs e)
{
if (currentPictureBox.ClientRectangle.Contains(e.Location))
{
((HandledMouseEventArgs)e).Handled = true;
currentPictureBox.Focus();
float zoomChange = e.Delta > 0 ? 2f : 0.5f;
// 計(jì)算縮放后圖片的大小
int newWidth = (int)(currentPictureBox.Width * zoomChange);
int newHeight = (int)(currentPictureBox.Height * zoomChange);
int newX = 0;
int newY = 0;
if (e.Delta > 0)
{
newX = (int)(currentPictureBox.Left - e.X);
newY = (int)(currentPictureBox.Top - e.Y);
}
else
{
newX = (int)(currentPictureBox.Left + e.X / 2);
newY = (int)(currentPictureBox.Top + e.Y / 2);
}
currentPictureBox.Size = new Size(newWidth, newHeight);
currentPictureBox.Location = new Point(newX, newY);
}
}
private void pictureBox_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
mouseDownPoint.X = Cursor.Position.X;
mouseDownPoint.Y = Cursor.Position.Y;
if (textMode)
{
Cursor.Current = Cursors.Cross;
/*
TextBox textBox = new TextBox();
currentTextBox = textBox;
currentPictureBox.Controls.Add(textBox);
textBox.Location = new Point(mouseDownPoint.X, mouseDownPoint.Y);
textBox.Size = new Size(0, 0);*/
}
else {
Cursor.Current = Cursors.SizeAll;
isSelected = true;
}
}
}
private void pictureBox_MouseMove(object sender, MouseEventArgs e)
{
if (textMode) {
if (currentTextBox != null) {
// currentTextBox.Width = currentTextBox.Width + (Cursor.Position.X - mouseDownPoint.X);
// currentTextBox.Height = currentTextBox.Height + (Cursor.Position.Y - mouseDownPoint.Y);
}
mouseDownPoint.X = Cursor.Position.X;
mouseDownPoint.Y = Cursor.Position.Y;
}
else if (isSelected)
{
currentPictureBox.Left = currentPictureBox.Left + (Cursor.Position.X - mouseDownPoint.X);
currentPictureBox.Top = currentPictureBox.Top + (Cursor.Position.Y - mouseDownPoint.Y);
mouseDownPoint.X = Cursor.Position.X;
mouseDownPoint.Y = Cursor.Position.Y;
}
}
private void pictureBox_MouseUp(object sender, MouseEventArgs e)
{
isSelected = false;
// textMode = false;
}
/*************************裁切平面鼠標(biāo)事件*************************/
private void clip_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
mouseDownPoint.X = Cursor.Position.X;
mouseDownPoint.Y = Cursor.Position.Y;
isClicked = true;
}
}
private void clip_MouseMove(object sender, MouseEventArgs e)
{
if (isClicked)
{
int dotaX = mouseDownPoint.X - Cursor.Position.X;
int dotaY = mouseDownPoint.Y - Cursor.Position.Y;
currentClipPanel.Left = currentClipPanel.Left - dotaX;
currentClipPanel.Top = currentClipPanel.Top - dotaY;
// 刷新第一寇蚊、二、三棍好、四個(gè)點(diǎn)位置
int newOneX = currentClipPanel.Location.X + currentClipPanel.Width / 2 - pointRadis / 2;
int newTwoY = currentClipPanel.Location.Y + currentClipPanel.Height / 2 - pointRadis / 2;
OneButton.Location = new Point(newOneX, currentClipPanel.Location.Y - pointRadis / 2);
ThreeButton.Location = new Point(newOneX, currentClipPanel.Location.Y + currentClipPanel.Height - pointRadis / 2);
TwoButton.Location = new Point(currentClipPanel.Location.X + currentClipPanel.Width - pointRadis / 2, newTwoY);
FourButton.Location = new Point(currentClipPanel.Location.X - pointRadis / 2, newTwoY);
mouseDownPoint.X = Cursor.Position.X;
mouseDownPoint.Y = Cursor.Position.Y;
}
}
private void clip_MouseUp(object sender, MouseEventArgs e)
{
isClicked = false;
}
/*************************第一個(gè)點(diǎn)鼠標(biāo)事件*************************/
private void One_MouseDown(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Left)
{
mouseDownPoint.X = Cursor.Position.X;
mouseDownPoint.Y = Cursor.Position.Y;
Cursor.Current = Cursors.SizeNS;
isClickedOne = true;
}
}
private void One_MouseMove(object sender, MouseEventArgs e)
{
if (isClickedOne) {
int dotaY = mouseDownPoint.Y - Cursor.Position.Y;
if (ThreeButton.Location.Y - OneButton.Location.Y > 50 || dotaY > 0) {
// 計(jì)算高度和位置
currentClipPanel.Height = currentClipPanel.Height + dotaY;
int newY = currentClipPanel.Location.Y - dotaY;
currentClipPanel.Location = new Point(currentClipPanel.Location.X, newY);
int newPointY = OneButton.Location.Y - dotaY;
OneButton.Location = new Point(OneButton.Location.X, newPointY);
// 刷新第二仗岸、四個(gè)點(diǎn)位置
int newTwoY = currentClipPanel.Location.Y + currentClipPanel.Height / 2 - pointRadis / 2;
TwoButton.Location = new Point(TwoButton.Location.X, newTwoY);
FourButton.Location = new Point(FourButton.Location.X, newTwoY);
mouseDownPoint.X = Cursor.Position.X;
mouseDownPoint.Y = Cursor.Position.Y;
}
}
}
private void One_MouseUp(object sender, MouseEventArgs e)
{
isClickedOne = false;
}
/*************************第二個(gè)點(diǎn)鼠標(biāo)事件*************************/
private void Two_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Cursor.Current = Cursors.SizeWE;
mouseDownPoint.X = Cursor.Position.X;
mouseDownPoint.Y = Cursor.Position.Y;
isClickedTwo = true;
}
}
private void Two_MouseMove(object sender, MouseEventArgs e)
{
if (isClickedTwo)
{
int dotaX = mouseDownPoint.X - Cursor.Position.X;
if (TwoButton.Location.X - FourButton.Location.X > 50 || dotaX < 0) {
// 計(jì)算寬度和位置
currentClipPanel.Width -= dotaX;
int newPointX = TwoButton.Location.X - dotaX;
TwoButton.Location = new Point(newPointX, TwoButton.Location.Y);
// 刷新第一、三個(gè)點(diǎn)位置
int newOneX = currentClipPanel.Location.X + currentClipPanel.Width / 2 - pointRadis / 2;
OneButton.Location = new Point(newOneX, OneButton.Location.Y);
ThreeButton.Location = new Point(newOneX, ThreeButton.Location.Y);
mouseDownPoint.X = Cursor.Position.X;
mouseDownPoint.Y = Cursor.Position.Y;
}
}
}
private void Two_MouseUp(object sender, MouseEventArgs e)
{
isClickedTwo = false;
}
/*************************第三個(gè)點(diǎn)鼠標(biāo)事件*************************/
private void Three_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Cursor.Current = Cursors.SizeNS;
mouseDownPoint.X = Cursor.Position.X;
mouseDownPoint.Y = Cursor.Position.Y;
isClickedThree = true;
}
}
private void Three_MouseMove(object sender, MouseEventArgs e)
{
if (isClickedThree)
{
int dotaY = mouseDownPoint.Y - Cursor.Position.Y;
if (ThreeButton.Location.Y - OneButton.Location.Y > 50 || dotaY < 0)
{
// 計(jì)算高度和位置
currentClipPanel.Height = currentClipPanel.Height - dotaY;
int newPointY = ThreeButton.Location.Y - dotaY;
ThreeButton.Location = new Point(ThreeButton.Location.X, newPointY);
// 刷新第二借笙、四個(gè)點(diǎn)位置
int newTwoY = currentClipPanel.Location.Y + currentClipPanel.Height / 2 - pointRadis / 2;
TwoButton.Location = new Point(TwoButton.Location.X, newTwoY);
FourButton.Location = new Point(FourButton.Location.X, newTwoY);
mouseDownPoint.X = Cursor.Position.X;
mouseDownPoint.Y = Cursor.Position.Y;
}
}
}
private void Three_MouseUp(object sender, MouseEventArgs e)
{
isClickedThree = false;
}
/*************************第四個(gè)點(diǎn)鼠標(biāo)事件*************************/
private void Four_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Cursor.Current = Cursors.SizeWE;
mouseDownPoint.X = Cursor.Position.X;
mouseDownPoint.Y = Cursor.Position.Y;
isClickedFour = true;
}
}
private void Four_MouseMove(object sender, MouseEventArgs e)
{
if (isClickedFour)
{
// 計(jì)算寬度和位置
int dotaX = mouseDownPoint.X - Cursor.Position.X;
if (TwoButton.Location.X - FourButton.Location.X > 50 || dotaX > 0)
{
currentClipPanel.Width += dotaX;
int newX = currentClipPanel.Location.X - dotaX;
currentClipPanel.Location = new Point(newX, currentClipPanel.Location.Y);
int newPointX = FourButton.Location.X - dotaX;
FourButton.Location = new Point(newPointX, FourButton.Location.Y);
// 刷新第一扒怖、三個(gè)點(diǎn)位置
int newOneX = currentClipPanel.Location.X + currentClipPanel.Width / 2 - pointRadis / 2;
OneButton.Location = new Point(newOneX, OneButton.Location.Y);
ThreeButton.Location = new Point(newOneX, ThreeButton.Location.Y);
mouseDownPoint.X = Cursor.Position.X;
mouseDownPoint.Y = Cursor.Position.Y;
}
}
}
private void Four_MouseUp(object sender, MouseEventArgs e)
{
isClickedFour = false;
}
/***************************功能方法***********************/
/**
* 繪制裁切框
*/
private void beginPainting()
{
Debug.WriteLine("====beginPainting====");
OneButton.Visible = clipMode;
TwoButton.Visible = clipMode;
ThreeButton.Visible = clipMode;
FourButton.Visible = clipMode;
currentPictureBox.Image = RotateImage(sourceImage, rotaNum);
currentPictureBox.Refresh();
// 初始化位置
setDefaultClipButtonPosition();
currentClipPanel.Visible = clipMode;
}
/**
* 結(jié)束繪制
*/
private void overPainting()
{
Debug.WriteLine("====overPainting====");
OneButton.Visible = clipMode;
TwoButton.Visible = clipMode;
ThreeButton.Visible = clipMode;
FourButton.Visible = clipMode;
currentPictureBox.Image = RotateImage(sourceImage, rotaNum);
currentImageBox.Refresh();
// 裁切
clipBitmap();
currentClipPanel.Visible = clipMode;
}
/**
* 初始化裁切框調(diào)整按鈕到圖像尺寸
*/
private static void setDefaultClipButtonPosition()
{
int halfButton = pointRadis / 2 ;
// 設(shè)置拖拉按鈕
one.X = currentPictureBox.Location.X + currentPictureBox.Width / 2 - halfButton;
one.Y = currentPictureBox.Location.Y - halfButton;
OneButton.Location = one;
two.X = currentPictureBox.Location.X + currentPictureBox.Width - halfButton;
two.Y = currentPictureBox.Location.Y + currentPictureBox.Height / 2 - halfButton;
TwoButton.Location = two;
three.X = one.X;
three.Y = currentPictureBox.Location.Y + currentPictureBox.Height - halfButton;
ThreeButton.Location = three;
four.X = currentPictureBox.Location.X - halfButton;
four.Y = two.Y;
FourButton.Location = four;
currentClipPanel.Location = currentPictureBox.Location;
currentClipPanel.Width = currentPictureBox.Width;
currentClipPanel.Height = currentPictureBox.Height;
Graphics graphicsClip = OneButton.CreateGraphics();
graphicsClip.DrawRectangle(clipPen, clipRect);
graphicsClip.Dispose();
}
/**
* 裁切圖片
*/
private static void clipBitmap() {
Image srcImage = currentPictureBox.Image;
int X = currentClipPanel.Location.X - currentPictureBox.Location.X;
int Y = currentClipPanel.Location.Y - currentPictureBox.Location.Y;
int crossWidth = X < 0 ? currentClipPanel.Width + X: currentClipPanel.Width;
int crossHeight = Y < 0 ? currentClipPanel.Height + Y : currentClipPanel.Height;
int Width = crossWidth * srcImage.Width / currentPictureBox.Width;
int Height = crossHeight * srcImage.Height / currentPictureBox.Height;
X = X < 0 ? 0 : X * srcImage.Width / currentPictureBox.Width;
Y = Y < 0 ? 0 : Y * srcImage.Height / currentPictureBox.Height;
// 創(chuàng)建新圖位圖
Bitmap bitmap = new Bitmap(Width, Height);
bitmap.SetResolution(srcImage.HorizontalResolution, srcImage.VerticalResolution);
// 創(chuàng)建作圖區(qū)域
Graphics graphic = Graphics.FromImage(bitmap);
graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
graphic.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Half;
// 截取原圖相應(yīng)區(qū)域?qū)懭胱鲌D區(qū)
Rectangle cRect = new Rectangle(X,Y, Width, Height);
graphic.DrawImage(srcImage, 0, 0, cRect, GraphicsUnit.Pixel);
graphic.Dispose();
/*Debug.WriteLine("SourceWidth===========>" + sourceImage.Width);
Debug.WriteLine("SourceHeight===========>" + sourceImage.Height);
Debug.WriteLine("Width===========>" + bitmap.Width);
Debug.WriteLine("Height===========>" + bitmap.Height);*/
//從作圖區(qū)生成新圖
// currentPictureBox.Image = Image.FromHbitmap( bitmap.GetHbitmap() );
/********刷新圖像*********/
currentPictureBox.Image = bitmap;
sourceImage = bitmap;
rotaNum = 0;
currentImageBox.Refresh();
clipRect.X = 0;
clipRect.Y = 0;
clipRect.Width = Width;
clipRect.Height = Height;
/********刷新圖像*********/
resetImageToPanel();
}
/**
* 旋轉(zhuǎn)圖像
*/
private static Bitmap RotateImage(Image image, float angle)
{
if (image == null)
{
throw new ArgumentNullException("image");
}
// 獲取中心點(diǎn)
float halfWidth = image.Width / 2.0f;
float halfHeight = image.Height / 2.0f;
// 弧度轉(zhuǎn)換
angle = angle % 360;
double radian = angle * Math.PI / 180.0;
double cos = Math.Cos(radian);
double sin = Math.Sin(radian);
int width = image.Width;
int height = image.Height;
// 計(jì)算旋轉(zhuǎn)后的圖像容器長(zhǎng)寬
int resultWidth = (int)(Math.Max(Math.Abs(width * cos - height * sin), Math.Abs(width * cos + height * sin)));
int resultHeight = (int)(Math.Max(Math.Abs(width * sin - height * cos), Math.Abs(width * sin + height * cos)));
float centerX = resultWidth / 2;
float centerY = resultHeight / 2;
// 設(shè)置新的圖像容器
Bitmap newImage = new Bitmap(resultWidth, resultHeight);
newImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);
Graphics graphics = Graphics.FromImage(newImage);
// 平移圖像到中心點(diǎn)
graphics.TranslateTransform(centerX, centerY);
// 旋轉(zhuǎn)
graphics.RotateTransform(angle);
// 再次平移回來(lái)
graphics.TranslateTransform(-centerX, -centerY);
// 將圖片繪制到中心點(diǎn)上
float resultX = centerX - halfWidth;
float resultY = centerY - halfHeight;
graphics.DrawImage(image, new PointF(resultX, resultY));
graphics.Dispose();
clipRect.X = 0;
clipRect.Y = 0;
clipRect.Width = resultWidth;
clipRect.Height = resultHeight;
resetImageToPanel();
// 如果是裁切將顯示裁切框
/*if (clipMode)
{
Graphics graphicsClip = Graphics.FromImage(newImage);
graphicsClip.DrawRectangle(rotaPen, clipRect);
graphicsClip.Dispose();
}*/
/*Graphics graphicsClip = Graphics.FromImage(newImage);
graphicsClip.DrawRectangle(rotaPen, clipRect);
graphicsClip.Dispose();*/
return newImage;
}
/**
* 水平鏡像
*/
private static Image turnOverImage(Image image, string mode) {
Graphics g = Graphics.FromImage(image);
Rectangle rect = new Rectangle(0, 0, currentPictureBox.Image.Width, currentPictureBox.Image.Height);
switch (mode) {
case "mirror":
image.RotateFlip(RotateFlipType.RotateNoneFlipX);
break;
case "vertical":
image.RotateFlip(RotateFlipType.RotateNoneFlipY);
break;
}
g.DrawImage(image, rect);
g.Dispose();
return image;
}
}
}
目前只實(shí)現(xiàn)了這些,效率和內(nèi)存方面還沒有進(jìn)行測(cè)試业稼,計(jì)劃添加文字標(biāo)注和圖片導(dǎo)出功能盗痒。
代碼地址:https://github.com/suojianwen95/ImageEditer.git