从 winform 到 WPF 再到 asp.net ,这一切都使我获益良多。
QQ : 1176156504
微信 : s1176156504
邮箱 : zidream@foxmail.com
所属程序集:ZnWpf.Presentation
本控件为自定义控件,实现了基本的图片浏览和编辑面板
注:本控件的编辑功能抽象层级较高,需具备一定的 MVVM 知识
依赖属性 | 类型 | 默认值 | 描述 |
---|---|---|---|
PanelWidth | double | 0 | 面板宽度 |
PanelHeight | double | 0 | 面板高度 |
SrcPath | string | string.Empty | 图片的完整路径 |
OriImage | BitmapImage | null | 图片 |
ImageMargin | Thickness | new Thickness(0,0,0,0) | 图片的坐标 |
ImageScale | double | 1 | 图片缩放倍率 |
<controls:ImageEditor Grid.Row="1" HorizontalAlignment="Left" VerticalAlignment="Top"
PanelHeight="{Binding PanelHeight}" PanelWidth="{Binding PanelWidth}" OriImage="{Binding Cover}" >
</controls:ImageEditor>
public class CoverEditViewModel : ObservableObject
{
private double panelHeight;
public double PanelHeight
{
get { return panelHeight; }
set => SetProperty(ref panelHeight, value);
}
private double panelWidth;
public double PanelWidth
{
get { return panelWidth; }
set => SetProperty(ref panelWidth, value);
}
private BitmapImage? cover;
public BitmapImage? Cover
{
get { return cover; }
set => SetProperty(ref cover, value);
}
}
浏览图片,只需要使用 PanelHeight, PanelWidth, OriImage 三个属性即可
<controls:ImageEditor x:Name="ImageViewer" Grid.Row="1" HorizontalAlignment="Left" VerticalAlignment="Top"
PanelHeight="{Binding PanelHeight}" PanelWidth="{Binding PanelWidth}"
SrcPath="{Binding CoverPath}" OriImage="{Binding Cover}" >
</controls:ImageEditor>
public void SaveCover()
{
Bitmap bitmap = new Bitmap(ImageViewer.SrcPath);
double correct = bitmap.Height / ImageViewer.OriImage.Height;
int x = (int)(ImageViewer.ImageMargin.Left / ImageViewer.ImageScale * correct);
int y = (int)(ImageViewer.ImageMargin.Top / ImageViewer.ImageScale * correct);
int height = (int)(ImageViewer.PanelHeight / ImageViewer.ImageScale * correct);
int width = (int)(ImageViewer.PanelWidth / ImageViewer.ImageScale * correct);
var result = bitmap.Clone(new System.Drawing.Rectangle(-x, -y, width, height), System.Drawing.Imaging.PixelFormat.Format32bppArgb);
if (result != null && DestPath != string.Empty)
{
result.Save(DestPath, System.Drawing.Imaging.ImageFormat.Png);
}
}
裁切图片除 PanelHeight, PanelWidth, OriImage 三个属性外,还需提供 SrcPath