Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
Kivii.Client.Sample.ImageUploader.V4.5
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
陶然
Kivii.Client.Sample.ImageUploader.V4.5
Commits
8e27b1a4
Commit
8e27b1a4
authored
Jun 15, 2024
by
陶然
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
优化裁剪功能
parent
8fadbd7d
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
226 additions
and
432 deletions
+226
-432
ImageResizer.cs
Src/Controls/ImageResizer.cs
+0
-212
PanelEx.cs
Src/Controls/PanelEx.cs
+155
-0
FrmImageEditor.Designer.cs
Src/FrmImageEditor.Designer.cs
+37
-33
FrmImageEditor.cs
Src/FrmImageEditor.cs
+32
-185
FrmMain.Designer.cs
Src/FrmMain.Designer.cs
+0
-0
Kivii.Client.Sample.ImageUploader.V4.5.csproj
Src/Kivii.Client.Sample.ImageUploader.V4.5.csproj
+2
-2
No files found.
Src/Controls/ImageResizer.cs
deleted
100644 → 0
View file @
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);
}
}
}
Src/Controls/PanelEx.cs
0 → 100644
View file @
8e27b1a4
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
;
}
}
}
Src/FrmImageEditor.Designer.cs
View file @
8e27b1a4
...
...
@@ -34,10 +34,10 @@
this
.
plControl
=
new
System
.
Windows
.
Forms
.
Panel
();
this
.
btnSave
=
new
System
.
Windows
.
Forms
.
Button
();
this
.
btnCancel
=
new
System
.
Windows
.
Forms
.
Button
();
this
.
p
lImage
=
new
System
.
Windows
.
Forms
.
Panel
();
this
.
imgResizer
=
new
Kivii
.
Sample
.
ImageUploader
.
Controls
.
ImageResizer
();
this
.
p
anel
=
new
System
.
Windows
.
Forms
.
Panel
();
this
.
pictureBox
=
new
Kivii
.
Sample
.
ImageUploader
.
Controls
.
PanelEx
();
this
.
plControl
.
SuspendLayout
();
this
.
p
lImage
.
SuspendLayout
();
this
.
p
anel
.
SuspendLayout
();
this
.
SuspendLayout
();
//
// timeImageEditor
...
...
@@ -50,10 +50,9 @@
this
.
plControl
.
Controls
.
Add
(
this
.
btnSave
);
this
.
plControl
.
Controls
.
Add
(
this
.
btnCancel
);
this
.
plControl
.
Dock
=
System
.
Windows
.
Forms
.
DockStyle
.
Bottom
;
this
.
plControl
.
Location
=
new
System
.
Drawing
.
Point
(
0
,
514
);
this
.
plControl
.
Margin
=
new
System
.
Windows
.
Forms
.
Padding
(
2
);
this
.
plControl
.
Location
=
new
System
.
Drawing
.
Point
(
0
,
771
);
this
.
plControl
.
Name
=
"plControl"
;
this
.
plControl
.
Size
=
new
System
.
Drawing
.
Size
(
748
,
10
0
);
this
.
plControl
.
Size
=
new
System
.
Drawing
.
Size
(
1122
,
15
0
);
this
.
plControl
.
TabIndex
=
0
;
//
// btnSave
...
...
@@ -64,8 +63,9 @@
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
.
Location
=
new
System
.
Drawing
.
Point
(
0
,
0
);
this
.
btnSave
.
Margin
=
new
System
.
Windows
.
Forms
.
Padding
(
4
);
this
.
btnSave
.
Name
=
"btnSave"
;
this
.
btnSave
.
Size
=
new
System
.
Drawing
.
Size
(
510
,
10
0
);
this
.
btnSave
.
Size
=
new
System
.
Drawing
.
Size
(
765
,
15
0
);
this
.
btnSave
.
TabIndex
=
5
;
this
.
btnSave
.
Text
=
"保存"
;
this
.
btnSave
.
UseVisualStyleBackColor
=
false
;
...
...
@@ -76,50 +76,53 @@
this
.
btnCancel
.
BackColor
=
System
.
Drawing
.
SystemColors
.
ButtonHighlight
;
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
.
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
.
Size
=
new
System
.
Drawing
.
Size
(
238
,
10
0
);
this
.
btnCancel
.
Size
=
new
System
.
Drawing
.
Size
(
357
,
15
0
);
this
.
btnCancel
.
TabIndex
=
4
;
this
.
btnCancel
.
Text
=
"取消"
;
this
.
btnCancel
.
UseVisualStyleBackColor
=
false
;
this
.
btnCancel
.
Click
+=
new
System
.
EventHandler
(
this
.
btnCancel_Click
);
//
// p
lImage
// p
anel
//
this
.
plImage
.
BackgroundImageLayout
=
System
.
Windows
.
Forms
.
ImageLayout
.
Center
;
this
.
plImage
.
Controls
.
Add
(
this
.
imgResizer
);
this
.
plImage
.
Dock
=
System
.
Windows
.
Forms
.
DockStyle
.
Fill
;
this
.
plImage
.
Location
=
new
System
.
Drawing
.
Point
(
0
,
0
);
this
.
plImage
.
Name
=
"plImage"
;
this
.
plImage
.
Size
=
new
System
.
Drawing
.
Size
(
748
,
514
);
this
.
plImage
.
TabIndex
=
2
;
this
.
panel
.
AutoScroll
=
true
;
this
.
panel
.
BackgroundImageLayout
=
System
.
Windows
.
Forms
.
ImageLayout
.
Center
;
this
.
panel
.
Controls
.
Add
(
this
.
pictureBox
);
this
.
panel
.
Dock
=
System
.
Windows
.
Forms
.
DockStyle
.
Fill
;
this
.
panel
.
Location
=
new
System
.
Drawing
.
Point
(
0
,
0
);
this
.
panel
.
Margin
=
new
System
.
Windows
.
Forms
.
Padding
(
4
);
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
.
imgResizer
.
Location
=
new
System
.
Drawing
.
Point
(
151
,
11
);
this
.
imgResizer
.
Margin
=
new
System
.
Windows
.
Forms
.
Padding
(
2
);
this
.
imgResizer
.
Name
=
"imgResizer"
;
this
.
imgResizer
.
Size
=
new
System
.
Drawing
.
Size
(
430
,
498
);
this
.
imgResizer
.
TabIndex
=
1
;
this
.
imgResizer
.
Text
=
"imgResizer"
;
this
.
pictureBox
.
BackgroundImageLayout
=
System
.
Windows
.
Forms
.
ImageLayout
.
Zoom
;
this
.
pictureBox
.
BorderStyle
=
System
.
Windows
.
Forms
.
BorderStyle
.
FixedSingle
;
this
.
pictureBox
.
EnableSelection
=
false
;
this
.
pictureBox
.
Location
=
new
System
.
Drawing
.
Point
(
250
,
253
);
this
.
pictureBox
.
Name
=
"pictureBox"
;
this
.
pictureBox
.
SelectionBounds
=
new
System
.
Drawing
.
Rectangle
(
0
,
0
,
0
,
0
);
this
.
pictureBox
.
Size
=
new
System
.
Drawing
.
Size
(
38
,
32
);
this
.
pictureBox
.
TabIndex
=
0
;
//
// FrmImageEditor
//
this
.
AutoScaleDimensions
=
new
System
.
Drawing
.
SizeF
(
6F
,
12
F
);
this
.
AutoScaleDimensions
=
new
System
.
Drawing
.
SizeF
(
9F
,
18
F
);
this
.
AutoScaleMode
=
System
.
Windows
.
Forms
.
AutoScaleMode
.
Font
;
this
.
ClientSize
=
new
System
.
Drawing
.
Size
(
748
,
614
);
this
.
Controls
.
Add
(
this
.
p
lImage
);
this
.
ClientSize
=
new
System
.
Drawing
.
Size
(
1122
,
921
);
this
.
Controls
.
Add
(
this
.
p
anel
);
this
.
Controls
.
Add
(
this
.
plControl
);
this
.
Icon
=
((
System
.
Drawing
.
Icon
)(
resources
.
GetObject
(
"$this.Icon"
)));
this
.
Margin
=
new
System
.
Windows
.
Forms
.
Padding
(
2
);
this
.
MinimumSize
=
new
System
.
Drawing
.
Size
(
600
,
530
);
this
.
MinimumSize
=
new
System
.
Drawing
.
Size
(
889
,
767
);
this
.
Name
=
"FrmImageEditor"
;
this
.
Text
=
"图片裁剪"
;
this
.
Load
+=
new
System
.
EventHandler
(
this
.
FrmImageEditor_Load
);
this
.
Resize
+=
new
System
.
EventHandler
(
this
.
FrmImageEditor_Resize
);
this
.
plControl
.
ResumeLayout
(
false
);
this
.
p
lImage
.
ResumeLayout
(
false
);
this
.
p
anel
.
ResumeLayout
(
false
);
this
.
ResumeLayout
(
false
);
}
...
...
@@ -130,7 +133,7 @@
private
System
.
Windows
.
Forms
.
Panel
plControl
;
private
System
.
Windows
.
Forms
.
Button
btnSave
;
private
System
.
Windows
.
Forms
.
Button
btnCancel
;
private
Controls
.
ImageResizer
imgResizer
;
private
System
.
Windows
.
Forms
.
Panel
plImage
;
private
System
.
Windows
.
Forms
.
Panel
panel
;
private
Controls
.
PanelEx
pictureBox
;
}
}
\ No newline at end of file
Src/FrmImageEditor.cs
View file @
8e27b1a4
...
...
@@ -20,28 +20,13 @@ namespace Kivii.Sample.ImageUploader
public
FrmImageEditor
()
{
InitializeComponent
();
pictureBox
.
SelectionBounds
=
new
Rectangle
(
0
,
0
,
500
,
600
);
}
public
Bitmap
backgroundImage
;
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
)
{
FrmMain
main
=
(
FrmMain
)
this
.
Owner
;
...
...
@@ -51,9 +36,9 @@ namespace Kivii.Sample.ImageUploader
private
void
FrmImageEditor_Load
(
object
sender
,
EventArgs
e
)
{
this
.
plImage
.
BackgroundImage
=
backgroundImage
;
this
.
plImage
.
BackgroundImageLayout
=
ImageLayout
.
Zoom
;
ImgEdited
=
backgroundImag
e
;
pictureBox
.
BackgroundImage
=
backgroundImage
;
pictureBox
.
BackgroundImageLayout
=
ImageLayout
.
Zoom
;
pictureBox
.
EnableSelection
=
tru
e
;
resizePictureBox
();
}
private
void
btnCancel_Click
(
object
sender
,
EventArgs
e
)
...
...
@@ -62,44 +47,27 @@ namespace Kivii.Sample.ImageUploader
}
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
);
var
imgArea
=
imgResizer
.
area
;
Rectangle
area
=
new
Rectangle
(
p
,
imgArea
.
Size
);
ImgEdited
=
GetPartOfImage
(
backgroundImage
,
area
.
Width
,
area
.
Height
,
p
.
X
,
p
.
Y
);
ImgEdited
=
new
Bitmap
(
GetSelectionImage
());
Bitmap
bmpFluffy
=
ImgEdited
;
//new Bitmap(after);
Rectangle
r
=
new
Rectangle
(
System
.
Drawing
.
Point
.
Empty
,
bmpFluffy
.
Size
);
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
}))
pictureBox
.
Refresh
();
timeImageEditor
.
Enabled
=
true
;
}
private
void
FrmImageEditor_Resize
(
object
sender
,
EventArgs
e
)
{
g
.
FillRectangle
(
Brushes
.
Black
,
r
);
g
.
SmoothingMode
=
SmoothingMode
.
HighQuality
;
g
.
FillPath
(
brush
,
path
);
transferOneARGBChannelFromOneBitmapToAnother
(
bmpMask
,
bmpFluffy
,
ChannelARGB
.
Blue
,
ChannelARGB
.
Alpha
);
resizePictureBox
();
}
Graphics
drawBk
=
Graphics
.
FromImage
(
backgroundImage
);
drawBk
.
Clear
(
Color
.
FromKnownColor
(
KnownColor
.
Control
));
drawBk
.
DrawImage
(
ImgEdited
,
area
);
Graphics
winGph
=
Graphics
.
FromHwnd
(
this
.
Handle
);
winGph
.
DrawImage
(
backgroundImage
,
new
System
.
Drawing
.
Point
(
0
,
0
));
this
.
plImage
.
BackgroundImage
=
backgroundImage
;
imgResizer
.
Visible
=
false
;
this
.
plImage
.
Refresh
();
timeImageEditor
.
Enabled
=
true
;
#
region
返回图片
public
Image
GetSelectionImage
()
{
return
pictureBox
.
GetSelectionImage
(
_zoom
);
}
public
void
SetSelectionImage
(
Image
img
)
{
pictureBox
.
BackgroundImage
=
img
;
resizePictureBox
();
}
#
endregion
#
region
窗口大小发生变化
protected
override
void
OnResize
(
EventArgs
e
)
...
...
@@ -109,148 +77,27 @@ namespace Kivii.Sample.ImageUploader
}
private
void
resizePictureBox
()
{
if
(
p
lImage
.
BackgroundImage
==
null
)
return
;
if
(
p
ictureBox
.
BackgroundImage
==
null
)
return
;
int
width
,
height
;
if
(
_zoom
==
0f
)
{
float
wzoom
=
(
float
)
p
lImage
.
Width
/
(
float
)
plImage
.
BackgroundImage
.
Width
;
float
hzoom
=
(
float
)
p
lImage
.
Height
/
(
float
)
plImage
.
BackgroundImage
.
Height
;
float
wzoom
=
(
float
)
p
anel
.
Width
/
(
float
)
pictureBox
.
BackgroundImage
.
Width
;
float
hzoom
=
(
float
)
p
anel
.
Height
/
(
float
)
pictureBox
.
BackgroundImage
.
Height
;
_zoom
=
wzoom
<
hzoom
?
wzoom
:
hzoom
;
}
else
if
(
_zoom
>
5f
)
_zoom
=
5f
;
else
if
(
_zoom
<
0.1f
)
_zoom
=
0.1f
;
width
=
(
int
)((
float
)
p
lImage
.
BackgroundImage
.
Width
*
_zoom
);
height
=
(
int
)((
float
)
p
lImage
.
BackgroundImage
.
Height
*
_zoom
);
p
lImage
.
Width
=
width
;
p
lImage
.
Height
=
height
;
width
=
(
int
)((
float
)
p
ictureBox
.
BackgroundImage
.
Width
*
_zoom
);
height
=
(
int
)((
float
)
p
ictureBox
.
BackgroundImage
.
Height
*
_zoom
);
p
ictureBox
.
Width
=
width
;
p
ictureBox
.
Height
=
height
;
int
x
,
y
;
if
(
plImage
.
Width
<=
width
)
x
=
0
;
else
x
=
(
plImage
.
Width
-
width
)
/
2
;
if
(
plImage
.
Height
<=
height
)
y
=
0
;
else
y
=
(
plImage
.
Height
-
height
)
/
2
;
plImage
.
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
);
if
(
panel
.
Width
<=
width
)
x
=
0
;
else
x
=
(
panel
.
Width
-
width
)
/
2
;
if
(
panel
.
Height
<=
height
)
y
=
0
;
else
y
=
(
panel
.
Height
-
height
)
/
2
;
pictureBox
.
Location
=
new
System
.
Drawing
.
Point
(
x
,
y
);
}
#
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
();
}
}
}
Src/FrmMain.Designer.cs
View file @
8e27b1a4
This diff is collapsed.
Click to expand it.
Src/Kivii.Client.Sample.ImageUploader.V4.5.csproj
View file @
8e27b1a4
...
...
@@ -75,7 +75,7 @@
</ItemGroup>
<ItemGroup>
<Compile
Include=
"Configs.cs"
/>
<Compile
Include=
"Controls\
ImageResizer
.cs"
>
<Compile
Include=
"Controls\
PanelEx
.cs"
>
<SubType>
Component
</SubType>
</Compile>
<Compile
Include=
"Controls\VideoSourcePlayer.cs"
>
...
...
@@ -136,7 +136,7 @@
<Content
Include=
"Camera.ico"
/>
</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>
<Name>
Kivii.Client.Canon.V4.5
</Name>
</ProjectReference>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment