Commit 8e27b1a4 by 陶然

优化裁剪功能

parent 8fadbd7d
using System.Drawing;
using System.Windows.Forms;
namespace Kivii.Sample.ImageUploader.Controls
{
public class ImageResizer : Control
{
public string fileName;
public ImageResizer()
{
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
//this.BackColor = Color.Empty;
this.BackColor = Color.Transparent;
//this.BackColor = Color.FromArgb(50, Color.Red);
//以下是设置双缓冲的代码 要不然在下面onPaint里的绘制半透明背景会让你很伤心,或者上面那句设置半透明背景用了过后拖动时也会让你跟蜗牛一样。
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景.
this.SetStyle(ControlStyles.DoubleBuffer, true);
}
public Size border;
private System.Drawing.Point m_MousePoint;
private Size m_Size;
private System.Drawing.Point m_LastPoint;
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
this.m_LastPoint = this.Location;
this.m_MousePoint = this.PointToScreen(e.Location);
this.m_Size = this.Size;
//this.BackColor = Color.Empty;
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if (arow != arrowType.none)
{
ReResize(e);
return;
}
if (e.Y <= recArrow[4].Bottom && e.Y >= recArrow[4].Top && e.X >= recArrow[4].Left && e.X <= recArrow[4].Right)//左上
Cursor = Cursors.SizeNWSE;
else if (e.Y <= recArrow[5].Bottom && e.Y >= recArrow[5].Top && e.X >= recArrow[5].Left && e.X <= recArrow[5].Right)//左下
Cursor = Cursors.SizeNESW;
else if (e.Y <= recArrow[6].Bottom && e.Y >= recArrow[6].Top && e.X >= recArrow[6].Left && e.X <= recArrow[6].Right)//右上
Cursor = Cursors.SizeNESW;
else if (e.Y <= recArrow[7].Bottom && e.Y >= recArrow[7].Top && e.X >= recArrow[7].Left && e.X <= recArrow[7].Right)//右下
Cursor = Cursors.SizeNWSE;
else if (e.Y <= recArrow[0].Bottom && e.Y >= recArrow[0].Top)//上
Cursor = Cursors.SizeNS;
else if (e.Y <= recArrow[1].Bottom && e.Y >= recArrow[1].Top)//下
Cursor = Cursors.SizeNS;
else if (e.X >= recArrow[2].Left && e.X <= recArrow[2].Right)//左
Cursor = Cursors.SizeWE;
else if (e.X >= recArrow[3].Left && e.X <= recArrow[3].Right)//右
Cursor = Cursors.SizeWE;
else
Cursor = Cursors.SizeAll;
if (e.Button == MouseButtons.Left)
{
System.Drawing.Point t = this.PointToScreen(e.Location);
System.Drawing.Point l = this.m_LastPoint;
if (e.Y <= recArrow[4].Bottom && e.Y >= recArrow[4].Top && e.X >= recArrow[4].Left && e.X <= recArrow[4].Right)//左上
arow = arrowType.leftUp;
else if (e.Y <= recArrow[5].Bottom && e.Y >= recArrow[5].Top && e.X >= recArrow[5].Left && e.X <= recArrow[5].Right)//左下
arow = arrowType.leftDown;
else if (e.Y <= recArrow[6].Bottom && e.Y >= recArrow[6].Top && e.X >= recArrow[6].Left && e.X <= recArrow[6].Right)//右上
arow = arrowType.rightUp;
else if (e.Y <= recArrow[7].Bottom && e.Y >= recArrow[7].Top && e.X >= recArrow[7].Left && e.X <= recArrow[7].Right)//右下
arow = arrowType.rightDown;
else if (e.Y <= recArrow[0].Bottom && e.Y >= recArrow[0].Top)//上
arow = arrowType.up;
else if (e.Y <= recArrow[1].Bottom && e.Y >= recArrow[1].Top)//下
arow = arrowType.down;
else if (e.X >= recArrow[2].Left && e.X <= recArrow[2].Right)//左
arow = arrowType.left;
else if (e.X >= recArrow[3].Left && e.X <= recArrow[3].Right)//右
arow = arrowType.right;
else
arow = arrowType.none;
l.Offset(t.X - this.m_MousePoint.X, t.Y - this.m_MousePoint.Y);
if (arow != arrowType.none)
ReResize(e);
else
{
this.Location = l;
Refresh();//这句很重要立即重绘 不然拖动到时候会出现卡卡 的现象 ,找了半天原因
}
}
}
public void ReResize(MouseEventArgs e)
{
System.Drawing.Point t = this.PointToScreen(e.Location);
System.Drawing.Point l = this.m_LastPoint;
l.Offset(t.X - this.m_MousePoint.X, t.Y - this.m_MousePoint.Y);
switch (arow)
{
case arrowType.up:
{
this.Height = m_Size.Height - (t.Y - this.m_MousePoint.Y);
this.Location = new System.Drawing.Point(m_LastPoint.X, l.Y);
break;
}
case arrowType.down:
{
this.Height = m_Size.Height + (t.Y - this.m_MousePoint.Y);
break;
}
case arrowType.left:
{
this.Width = m_Size.Width - (t.X - this.m_MousePoint.X);
this.Location = new System.Drawing.Point(l.X, m_LastPoint.Y);
break;
}
case arrowType.right:
{
this.Width = m_Size.Width + (t.X - this.m_MousePoint.X);
break;
}
case arrowType.leftUp:
{
this.Width = m_Size.Width - (t.X - this.m_MousePoint.X);
this.Height = m_Size.Height - (t.Y - this.m_MousePoint.Y);
this.Location = new System.Drawing.Point(l.X, l.Y);
break;
}
case arrowType.leftDown:
{
this.Width = m_Size.Width - (t.X - this.m_MousePoint.X);
this.Height = m_Size.Height + (t.Y - this.m_MousePoint.Y);
this.Location = new System.Drawing.Point(l.X, m_LastPoint.Y);
break;
}
case arrowType.rightUp:
{
this.Width = m_Size.Width + (t.X - this.m_MousePoint.X);
this.Height = m_Size.Height - (t.Y - this.m_MousePoint.Y);
this.Location = new System.Drawing.Point(m_LastPoint.X, l.Y);
break;
}
case arrowType.rightDown:
{
this.Width = m_Size.Width + (t.X - this.m_MousePoint.X);
this.Height = m_Size.Height + (t.Y - this.m_MousePoint.Y);
break;
}
}
this.Refresh();
}
public enum arrowType
{
up, down, left, right, leftUp, leftDown, rightUp, rightDown, none
}
public arrowType arow = arrowType.none;
Rectangle[] recArrow = new Rectangle[8];//8个手柄
public Rectangle area;//选择区域
public readonly int blank = 8;//边距
protected override void OnPaint(PaintEventArgs e)
{
int side = 6;//手柄矩形的边长
recArrow[0] = new Rectangle(new System.Drawing.Point(this.Width / 2 - side / 2, blank - side / 2), new Size(side, side));
recArrow[1] = new Rectangle(new System.Drawing.Point(this.Width / 2 - side / 2, this.Height - blank - side / 2), new Size(side, side));
recArrow[2] = new Rectangle(new System.Drawing.Point(blank - side / 2, this.Height / 2 - side / 2), new Size(side, side));
recArrow[3] = new Rectangle(new System.Drawing.Point(this.Width - blank - side / 2, this.Height / 2 - side / 2), new Size(side, side));
recArrow[4] = new Rectangle(new System.Drawing.Point(blank - side / 2, blank - side / 2), new Size(side, side));
recArrow[5] = new Rectangle(new System.Drawing.Point(blank - side / 2, this.Height - blank - side / 2), new Size(side, side));
recArrow[6] = new Rectangle(new System.Drawing.Point(this.Width - blank - side / 2, blank - side / 2), new Size(side, side));
recArrow[7] = new Rectangle(new System.Drawing.Point(this.Width - blank - side / 2, this.Height - blank - side / 2), new Size(side, side));
foreach (Rectangle item in recArrow)
e.Graphics.DrawRectangle(Pens.Red, item);
area = new Rectangle(new System.Drawing.Point(8, 8), new Size(this.Width - 8 * 2, this.Height - 8 * 2));
////加上半透明效果看倒是好看了 重绘的时候卡的凶 ,期待有解决方法
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
System.Drawing.Color cor = System.Drawing.Color.FromArgb(50, Color.Red);
System.Drawing.SolidBrush bsh = new System.Drawing.SolidBrush(cor);
e.Graphics.FillRectangle(bsh, area);
e.Graphics.DrawRectangle(Pens.Red, area);
}
protected override void OnMouseUp(MouseEventArgs e)
{
arow = arrowType.none;
//this.BackColor = Color.FromArgb(0, Color.Red);
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Kivii.Sample.ImageUploader.Controls
{
internal class PanelEx : Panel
{
#region 属性
private Rectangle _selectionBounds = Rectangle.Empty;
/// <summary>
/// 选择区域的XY
/// </summary>
public Rectangle SelectionBounds
{
get
{
return _selectionBounds;
}
set
{
_selectionBounds = value;
}
}
/// <summary>
/// 启用选择区域
/// </summary>
public bool EnableSelection { get; set; }
private Color _selectionBorderColor = Color.Red;
/// <summary>
/// 选择区域边界颜色
/// </summary>
[DefaultValue(typeof(Color), "Red")]
public Color SelectionBorderColor
{
get { return _selectionBorderColor; }
set { _selectionBorderColor = value; this.Invalidate(); }
}
private int _selectionBorderWidth = 2;
/// <summary>
/// 选择区域边界线粗细
/// </summary>
[DefaultValue(2)]
public int SelectionBorderWidth
{
get { return _selectionBorderWidth; }
set { _selectionBorderWidth = value; this.Invalidate(); }
}
/// <summary>
/// 起始偏移量
/// </summary>
private System.Drawing.Point _startPosition = new System.Drawing.Point(0, 0);
/// <summary>
/// 是否正在移动
/// </summary>
private int _movingStatus = 0;
#endregion
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (EnableSelection)
{
using (Pen p = new Pen(this.SelectionBorderColor, this.SelectionBorderWidth))
{
e.Graphics.DrawRectangle(p, SelectionBounds);
}
}
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
if (SelectionBounds.Contains(e.X, e.Y))
{
_startPosition = new System.Drawing.Point(e.X, e.Y);
_movingStatus = 1;
}
}
}
protected override void OnMouseMove(MouseEventArgs e)
{
if (_movingStatus == 0)
{
base.OnMouseMove(e);
return;
}
Rectangle rect;
if (_movingStatus == 2)
{
rect = new Rectangle(this.PointToScreen(_selectionBounds.Location), _selectionBounds.Size);
ControlPaint.DrawReversibleFrame(rect, Color.SkyBlue, FrameStyle.Thick);
}
_movingStatus = 2;
_selectionBounds.Offset(e.X - _startPosition.X, e.Y - _startPosition.Y);
_startPosition.X = e.X;
_startPosition.Y = e.Y;
if (_selectionBounds.X < 0)
{
_selectionBounds.X = 0;
}
if (_selectionBounds.Y < 0)
{
_selectionBounds.Y = 0;
}
if (_selectionBounds.Right > this.ClientSize.Width)
{
_selectionBounds.X = this.ClientSize.Width - _selectionBounds.Width;
}
if (_selectionBounds.Bottom > this.ClientSize.Height)
{
_selectionBounds.Y = this.ClientSize.Height - _selectionBounds.Height;
}
rect = new Rectangle(this.PointToScreen(_selectionBounds.Location), _selectionBounds.Size);
ControlPaint.DrawReversibleFrame(rect, Color.SkyBlue, FrameStyle.Thick);
this.Cursor = Cursors.SizeAll;
base.OnMouseMove(e);
}
protected override void OnMouseUp(MouseEventArgs e)
{
if (_movingStatus == 2) this.Invalidate();
_movingStatus = 0;
base.OnMouseUp(e);
}
public Image GetSelectionImage(float zoom)
{
if (this.BackgroundImage == null) return null;
if (!EnableSelection) return this.BackgroundImage;
Bitmap bmp = new Bitmap(_selectionBounds.Width, _selectionBounds.Height);
using (Graphics g = Graphics.FromImage(bmp))
{
g.DrawImage(
this.BackgroundImage,
new Rectangle(0, 0, bmp.Width, bmp.Height), _selectionBounds.X / zoom, _selectionBounds.Y / zoom, _selectionBounds.Width / zoom, _selectionBounds.Height / zoom, GraphicsUnit.Pixel);
}
return bmp;
}
}
}
...@@ -34,10 +34,10 @@ ...@@ -34,10 +34,10 @@
this.plControl = new System.Windows.Forms.Panel(); this.plControl = new System.Windows.Forms.Panel();
this.btnSave = new System.Windows.Forms.Button(); this.btnSave = new System.Windows.Forms.Button();
this.btnCancel = new System.Windows.Forms.Button(); this.btnCancel = new System.Windows.Forms.Button();
this.plImage = new System.Windows.Forms.Panel(); this.panel = new System.Windows.Forms.Panel();
this.imgResizer = new Kivii.Sample.ImageUploader.Controls.ImageResizer(); this.pictureBox = new Kivii.Sample.ImageUploader.Controls.PanelEx();
this.plControl.SuspendLayout(); this.plControl.SuspendLayout();
this.plImage.SuspendLayout(); this.panel.SuspendLayout();
this.SuspendLayout(); this.SuspendLayout();
// //
// timeImageEditor // timeImageEditor
...@@ -50,10 +50,9 @@ ...@@ -50,10 +50,9 @@
this.plControl.Controls.Add(this.btnSave); this.plControl.Controls.Add(this.btnSave);
this.plControl.Controls.Add(this.btnCancel); this.plControl.Controls.Add(this.btnCancel);
this.plControl.Dock = System.Windows.Forms.DockStyle.Bottom; this.plControl.Dock = System.Windows.Forms.DockStyle.Bottom;
this.plControl.Location = new System.Drawing.Point(0, 514); this.plControl.Location = new System.Drawing.Point(0, 771);
this.plControl.Margin = new System.Windows.Forms.Padding(2);
this.plControl.Name = "plControl"; this.plControl.Name = "plControl";
this.plControl.Size = new System.Drawing.Size(748, 100); this.plControl.Size = new System.Drawing.Size(1122, 150);
this.plControl.TabIndex = 0; this.plControl.TabIndex = 0;
// //
// btnSave // btnSave
...@@ -64,8 +63,9 @@ ...@@ -64,8 +63,9 @@
this.btnSave.Font = new System.Drawing.Font("宋体", 25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.btnSave.Font = new System.Drawing.Font("宋体", 25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.btnSave.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(128)))), ((int)(((byte)(128)))), ((int)(((byte)(255))))); this.btnSave.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(128)))), ((int)(((byte)(128)))), ((int)(((byte)(255)))));
this.btnSave.Location = new System.Drawing.Point(0, 0); this.btnSave.Location = new System.Drawing.Point(0, 0);
this.btnSave.Margin = new System.Windows.Forms.Padding(4);
this.btnSave.Name = "btnSave"; this.btnSave.Name = "btnSave";
this.btnSave.Size = new System.Drawing.Size(510, 100); this.btnSave.Size = new System.Drawing.Size(765, 150);
this.btnSave.TabIndex = 5; this.btnSave.TabIndex = 5;
this.btnSave.Text = "保存"; this.btnSave.Text = "保存";
this.btnSave.UseVisualStyleBackColor = false; this.btnSave.UseVisualStyleBackColor = false;
...@@ -76,50 +76,53 @@ ...@@ -76,50 +76,53 @@
this.btnCancel.BackColor = System.Drawing.SystemColors.ButtonHighlight; this.btnCancel.BackColor = System.Drawing.SystemColors.ButtonHighlight;
this.btnCancel.Dock = System.Windows.Forms.DockStyle.Right; this.btnCancel.Dock = System.Windows.Forms.DockStyle.Right;
this.btnCancel.Font = new System.Drawing.Font("宋体", 25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.btnCancel.Font = new System.Drawing.Font("宋体", 25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.btnCancel.Location = new System.Drawing.Point(510, 0); this.btnCancel.Location = new System.Drawing.Point(765, 0);
this.btnCancel.Margin = new System.Windows.Forms.Padding(4);
this.btnCancel.Name = "btnCancel"; this.btnCancel.Name = "btnCancel";
this.btnCancel.Size = new System.Drawing.Size(238, 100); this.btnCancel.Size = new System.Drawing.Size(357, 150);
this.btnCancel.TabIndex = 4; this.btnCancel.TabIndex = 4;
this.btnCancel.Text = "取消"; this.btnCancel.Text = "取消";
this.btnCancel.UseVisualStyleBackColor = false; this.btnCancel.UseVisualStyleBackColor = false;
this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click); this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
// //
// plImage // panel
// //
this.plImage.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center; this.panel.AutoScroll = true;
this.plImage.Controls.Add(this.imgResizer); this.panel.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;
this.plImage.Dock = System.Windows.Forms.DockStyle.Fill; this.panel.Controls.Add(this.pictureBox);
this.plImage.Location = new System.Drawing.Point(0, 0); this.panel.Dock = System.Windows.Forms.DockStyle.Fill;
this.plImage.Name = "plImage"; this.panel.Location = new System.Drawing.Point(0, 0);
this.plImage.Size = new System.Drawing.Size(748, 514); this.panel.Margin = new System.Windows.Forms.Padding(4);
this.plImage.TabIndex = 2; this.panel.Name = "panel";
this.panel.Size = new System.Drawing.Size(1122, 771);
this.panel.TabIndex = 2;
// //
// imgResizer // pictureBox
// //
this.imgResizer.BackColor = System.Drawing.Color.Transparent; this.pictureBox.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.imgResizer.Location = new System.Drawing.Point(151, 11); this.pictureBox.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.imgResizer.Margin = new System.Windows.Forms.Padding(2); this.pictureBox.EnableSelection = false;
this.imgResizer.Name = "imgResizer"; this.pictureBox.Location = new System.Drawing.Point(250, 253);
this.imgResizer.Size = new System.Drawing.Size(430, 498); this.pictureBox.Name = "pictureBox";
this.imgResizer.TabIndex = 1; this.pictureBox.SelectionBounds = new System.Drawing.Rectangle(0, 0, 0, 0);
this.imgResizer.Text = "imgResizer"; this.pictureBox.Size = new System.Drawing.Size(38, 32);
this.pictureBox.TabIndex = 0;
// //
// FrmImageEditor // FrmImageEditor
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 18F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(748, 614); this.ClientSize = new System.Drawing.Size(1122, 921);
this.Controls.Add(this.plImage); this.Controls.Add(this.panel);
this.Controls.Add(this.plControl); this.Controls.Add(this.plControl);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.Margin = new System.Windows.Forms.Padding(2); this.MinimumSize = new System.Drawing.Size(889, 767);
this.MinimumSize = new System.Drawing.Size(600, 530);
this.Name = "FrmImageEditor"; this.Name = "FrmImageEditor";
this.Text = "图片裁剪"; this.Text = "图片裁剪";
this.Load += new System.EventHandler(this.FrmImageEditor_Load); this.Load += new System.EventHandler(this.FrmImageEditor_Load);
this.Resize += new System.EventHandler(this.FrmImageEditor_Resize); this.Resize += new System.EventHandler(this.FrmImageEditor_Resize);
this.plControl.ResumeLayout(false); this.plControl.ResumeLayout(false);
this.plImage.ResumeLayout(false); this.panel.ResumeLayout(false);
this.ResumeLayout(false); this.ResumeLayout(false);
} }
...@@ -130,7 +133,7 @@ ...@@ -130,7 +133,7 @@
private System.Windows.Forms.Panel plControl; private System.Windows.Forms.Panel plControl;
private System.Windows.Forms.Button btnSave; private System.Windows.Forms.Button btnSave;
private System.Windows.Forms.Button btnCancel; private System.Windows.Forms.Button btnCancel;
private Controls.ImageResizer imgResizer; private System.Windows.Forms.Panel panel;
private System.Windows.Forms.Panel plImage; private Controls.PanelEx pictureBox;
} }
} }
\ No newline at end of file
...@@ -20,28 +20,13 @@ namespace Kivii.Sample.ImageUploader ...@@ -20,28 +20,13 @@ namespace Kivii.Sample.ImageUploader
public FrmImageEditor() public FrmImageEditor()
{ {
InitializeComponent(); InitializeComponent();
pictureBox.SelectionBounds = new Rectangle(0, 0, 500, 600);
} }
public Bitmap backgroundImage; public Bitmap backgroundImage;
public Bitmap ImgEdited; public Bitmap ImgEdited;
//图像裁剪
public Bitmap GetPartOfImage(Bitmap sourceImg, int width, int height, int offsetX, int offsetY)
{
Bitmap sourceBitmap = sourceImg;
Bitmap rtns = new Bitmap(width, height);
using (Graphics g = Graphics.FromImage(rtns))
{
Rectangle resultRectangle = new Rectangle(0, 0, width, height);
Rectangle sourceRectangle = new Rectangle(0 + offsetX, 0 + offsetY, width, height);
g.DrawImage(sourceBitmap, resultRectangle, sourceRectangle, GraphicsUnit.Pixel);
}
return rtns;
}
private void timeImageEditor_Tick(object sender, EventArgs e) private void timeImageEditor_Tick(object sender, EventArgs e)
{ {
FrmMain main = (FrmMain)this.Owner; FrmMain main = (FrmMain)this.Owner;
...@@ -51,9 +36,9 @@ namespace Kivii.Sample.ImageUploader ...@@ -51,9 +36,9 @@ namespace Kivii.Sample.ImageUploader
private void FrmImageEditor_Load(object sender, EventArgs e) private void FrmImageEditor_Load(object sender, EventArgs e)
{ {
this.plImage.BackgroundImage = backgroundImage; pictureBox.BackgroundImage = backgroundImage;
this.plImage.BackgroundImageLayout = ImageLayout.Zoom; pictureBox.BackgroundImageLayout = ImageLayout.Zoom;
ImgEdited = backgroundImage; pictureBox.EnableSelection = true;
resizePictureBox(); resizePictureBox();
} }
private void btnCancel_Click(object sender, EventArgs e) private void btnCancel_Click(object sender, EventArgs e)
...@@ -62,44 +47,27 @@ namespace Kivii.Sample.ImageUploader ...@@ -62,44 +47,27 @@ namespace Kivii.Sample.ImageUploader
} }
private void btnSave_Click(object sender, EventArgs e) private void btnSave_Click(object sender, EventArgs e)
{ {
System.Drawing.Point p = new System.Drawing.Point(imgResizer.Location.X + imgResizer.blank, imgResizer.Location.Y + imgResizer.blank); ImgEdited = new Bitmap(GetSelectionImage());
var imgArea = imgResizer.area;
Rectangle area = new Rectangle(p, imgArea.Size);
ImgEdited = GetPartOfImage(backgroundImage, area.Width, area.Height, p.X, p.Y);
Bitmap bmpFluffy = ImgEdited; pictureBox.Refresh();
//new Bitmap(after); timeImageEditor.Enabled = true;
}
Rectangle r = new Rectangle(System.Drawing.Point.Empty, bmpFluffy.Size); private void FrmImageEditor_Resize(object sender, EventArgs e)
using (Bitmap bmpMask = new Bitmap(r.Width, r.Height))
using (Graphics g = Graphics.FromImage(bmpMask))
using (GraphicsPath path = createRoundRect(r.X, r.Y, r.Width, r.Height,0))
using (Brush brush = createFluffyBrush(path,new float[] { 0.0f, 0.1f, 1.0f },new float[] { 0.0f, 0.95f, 1.0f }))
{ {
g.FillRectangle(Brushes.Black, r); resizePictureBox();
g.SmoothingMode = SmoothingMode.HighQuality;
g.FillPath(brush, path);
transferOneARGBChannelFromOneBitmapToAnother(bmpMask,bmpFluffy,ChannelARGB.Blue,ChannelARGB.Alpha);
} }
Graphics drawBk = Graphics.FromImage(backgroundImage); #region 返回图片
drawBk.Clear(Color.FromKnownColor(KnownColor.Control)); public Image GetSelectionImage()
drawBk.DrawImage(ImgEdited, area); {
return pictureBox.GetSelectionImage(_zoom);
Graphics winGph = Graphics.FromHwnd(this.Handle); }
winGph.DrawImage(backgroundImage, new System.Drawing.Point(0, 0)); public void SetSelectionImage(Image img)
{
this.plImage.BackgroundImage = backgroundImage; pictureBox.BackgroundImage = img;
resizePictureBox();
imgResizer.Visible = false;
this.plImage.Refresh();
timeImageEditor.Enabled = true;
} }
#endregion
#region 窗口大小发生变化 #region 窗口大小发生变化
protected override void OnResize(EventArgs e) protected override void OnResize(EventArgs e)
...@@ -109,148 +77,27 @@ namespace Kivii.Sample.ImageUploader ...@@ -109,148 +77,27 @@ namespace Kivii.Sample.ImageUploader
} }
private void resizePictureBox() private void resizePictureBox()
{ {
if (plImage.BackgroundImage == null) return; if (pictureBox.BackgroundImage == null) return;
int width, height; int width, height;
if (_zoom == 0f) if (_zoom == 0f)
{ {
float wzoom = (float)plImage.Width / (float)plImage.BackgroundImage.Width; float wzoom = (float)panel.Width / (float)pictureBox.BackgroundImage.Width;
float hzoom = (float)plImage.Height / (float)plImage.BackgroundImage.Height; float hzoom = (float)panel.Height / (float)pictureBox.BackgroundImage.Height;
_zoom = wzoom < hzoom ? wzoom : hzoom; _zoom = wzoom < hzoom ? wzoom : hzoom;
} }
else if (_zoom > 5f) _zoom = 5f; else if (_zoom > 5f) _zoom = 5f;
else if (_zoom < 0.1f) _zoom = 0.1f; else if (_zoom < 0.1f) _zoom = 0.1f;
width = (int)((float)plImage.BackgroundImage.Width * _zoom); width = (int)((float)pictureBox.BackgroundImage.Width * _zoom);
height = (int)((float)plImage.BackgroundImage.Height * _zoom); height = (int)((float)pictureBox.BackgroundImage.Height * _zoom);
plImage.Width = width; pictureBox.Width = width;
plImage.Height = height; pictureBox.Height = height;
int x, y; int x, y;
if (plImage.Width <= width) x = 0; if (panel.Width <= width) x = 0;
else x = (plImage.Width - width) / 2; else x = (panel.Width - width) / 2;
if (plImage.Height <= height) y = 0; if (panel.Height <= height) y = 0;
else y = (plImage.Height - height) / 2; else y = (panel.Height - height) / 2;
plImage.Location = new System.Drawing.Point(x, y); pictureBox.Location = new System.Drawing.Point(x, y);
// 计算控件在父容器中居中的位置
int xi = (imgResizer.Parent.Width - imgResizer.Width) / 2;
int yi = (imgResizer.Parent.Height - imgResizer.Height) / 2;
imgResizer.Location= new System.Drawing.Point(xi, yi);
} }
#endregion #endregion
public static GraphicsPath createRoundRect(int x, int y, int width, int height, int radius)
{
GraphicsPath gp = new GraphicsPath();
if (radius == 0)
gp.AddRectangle(new Rectangle(x, y, width, height));
else
{
gp.AddLine(x + radius, y, x + width - radius, y);
gp.AddArc(x + width - radius, y, radius, radius, 270, 90);
gp.AddLine(x + width, y + radius, x + width, y + height - radius);
gp.AddArc(x + width - radius, y + height - radius, radius, radius, 0, 90);
gp.AddLine(x + width - radius, y + height, x + radius, y + height);
gp.AddArc(x, y + height - radius, radius, radius, 90, 90);
gp.AddLine(x, y + height - radius, x, y + radius);
gp.AddArc(x, y, radius, radius, 180, 90);
gp.CloseFigure();
}
return gp;
}
public static Brush createFluffyBrush(GraphicsPath gp, float[] blendPositions, float[] blendFactors)
{
PathGradientBrush pgb = new PathGradientBrush(gp);
//Blend blend = new Blend();
//blend.Positions = blendPositions;
//blend.Factors = blendFactors;
//pgb.Blend = blend;
//pgb.CenterColor = Color.White;
//pgb.SurroundColors = new Color[] { Color.Black };
return pgb;
}
public enum ChannelARGB
{
Blue = 0,
Green = 1,
Red = 2,
Alpha = 3
}
public static void transferOneARGBChannelFromOneBitmapToAnother(Bitmap source, Bitmap dest, ChannelARGB sourceChannel, ChannelARGB destChannel)
{
if (source.Size != dest.Size)
throw new ArgumentException();
Rectangle r = new Rectangle(System.Drawing.Point.Empty, source.Size);
BitmapData bdSrc = source.LockBits(r, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
BitmapData bdDst = dest.LockBits(r, ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
unsafe
{
byte* bpSrc = (byte*)bdSrc.Scan0.ToPointer();
byte* bpDst = (byte*)bdDst.Scan0.ToPointer();
bpSrc += (int)sourceChannel;
bpDst += (int)destChannel;
for (int i = r.Height * r.Width; i > 0; i--)
{
*bpDst = *bpSrc;
bpSrc += 4;
bpDst += 4;
}
}
source.UnlockBits(bdSrc);
dest.UnlockBits(bdDst);
}
private void FrmImageEditor_Resize(object sender, EventArgs e)
{
resizePictureBox();
}
} }
} }
...@@ -75,7 +75,7 @@ ...@@ -75,7 +75,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Configs.cs" /> <Compile Include="Configs.cs" />
<Compile Include="Controls\ImageResizer.cs"> <Compile Include="Controls\PanelEx.cs">
<SubType>Component</SubType> <SubType>Component</SubType>
</Compile> </Compile>
<Compile Include="Controls\VideoSourcePlayer.cs"> <Compile Include="Controls\VideoSourcePlayer.cs">
...@@ -136,7 +136,7 @@ ...@@ -136,7 +136,7 @@
<Content Include="Camera.ico" /> <Content Include="Camera.ico" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\Kivii.Client.Canon\Src\Kivii.Client.Canon.V4.5.csproj"> <ProjectReference Include="..\..\Kivii.Client.Canon.V4.5\Src\Kivii.Client.Canon.V4.5.csproj">
<Project>{08fa5081-59f1-4e6a-b44c-309ef67159e6}</Project> <Project>{08fa5081-59f1-4e6a-b44c-309ef67159e6}</Project>
<Name>Kivii.Client.Canon.V4.5</Name> <Name>Kivii.Client.Canon.V4.5</Name>
</ProjectReference> </ProjectReference>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment