using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using PaintDotNet.Base.SettingModel;
namespace PaintDotNet.GeneralAnalysis.Artwork
{
public partial class ArtworkUserControl : UserControl
{
private ArtworkModel.Item m_artwork;
private Bitmap m_artBitmap;
private Bitmap m_targetBitmap;
private Bitmap m_overlapBitmap;
public ArtworkUserControl()
{
InitializeComponent();
}
public virtual void Init()
{
}
public virtual bool ArtChanged { get; set; }
public virtual bool AuxiliaryLineEnabled { get; set; }
public virtual bool GridLineEnabled { get; set; }
public virtual int Transparency { get; set; }
public virtual int TargetTransparency { get; set; }
public virtual bool GenerateTargetPoint { get; set; }
public ArtworkModel.Item Artwork
{
set { this.m_artwork = value; }
get { return this.m_artwork; }
}
public Bitmap ArtBitmap
{
get
{
return this.m_artBitmap;
}
set
{
this.m_artBitmap = value;
BitmapArray[0] = this.m_artBitmap;
CalcOverlapWidthAndHeight();
Init();
}
}
public Bitmap TargetBitmap
{
get
{
return this.m_targetBitmap;
}
set
{
this.m_targetBitmap = value;
BitmapArray[1] = this.m_targetBitmap;
CalcOverlapWidthAndHeight();
Init();
}
}
public Bitmap TargetBitmapClone { get; set; }
public Bitmap[] BitmapArray { get; } = new Bitmap[2];
public int OverlapWidth { get; set; }
public int OverlapHeight { get; set; }
///
/// 工艺图和目标图拼合后的图片
///
public Bitmap OverlapBitmap
{
get
{
return this.m_overlapBitmap;
}
set
{
this.m_overlapBitmap = value;
Init();
}
}
///
/// 目标图移动的相对y坐标
///
public int TargetTop { get; set; } = 0;
///
/// 目标图移动的相对x坐标
///
public int TargetLeft { get; set; } = 0;
private void CalcOverlapWidthAndHeight()
{
OverlapWidth = 0;
OverlapHeight = 0;
if (ArtBitmap != null)
{
OverlapWidth = ArtBitmap.Width;
OverlapHeight = ArtBitmap.Height;
}
if (TargetBitmap != null)
{
OverlapWidth = OverlapWidth > TargetBitmap.Width ? OverlapWidth : TargetBitmap.Width;
OverlapHeight = OverlapHeight > TargetBitmap.Height ? OverlapHeight : TargetBitmap.Height;
}
}
public enum OverlapType
{
CENTER = 0, //中间重叠
TOPLEFT = 1, //左上角
}
public Bitmap Overlap(OverlapType type)
{
Bitmap[] bitmaps = this.BitmapArray;
int width = this.OverlapWidth;
int height = this.OverlapHeight;
if (width == 0 || height == 0) {
return null;
}
Bitmap bmPhoto = new Bitmap(width, height);
float x = 0;
float y = 0;
//load the Bitmap into a Graphics object
Graphics grPhoto = Graphics.FromImage(bmPhoto);
//Set the rendering quality for this Graphics object
grPhoto.SmoothingMode = SmoothingMode.AntiAlias;//清除锯齿的呈现
//haix
for (int i = 0; i < bitmaps.Length; i++)
{
Bitmap img = bitmaps[i];
if(img == null)
{
continue;
}
if (type == OverlapType.CENTER)
{
x = (width - img.Width) / 2.0f;
y = (height - img.Height) / 2.0f;
if (i == 1)
{
x += TargetLeft;
y += TargetTop;
}
}
grPhoto.DrawImage(img, new PointF(x,y));
}
return bmPhoto;
}
//定义图像透明度调整函数
public Bitmap MakeTransparent(Bitmap src, int num)
{
return ImageTools.MakeTransparent(src, num);
}
}
}