ArtworkUserControl.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Drawing.Drawing2D;
  11. using PaintDotNet.Base.SettingModel;
  12. namespace PaintDotNet.GeneralAnalysis.Artwork
  13. {
  14. public partial class ArtworkUserControl : UserControl
  15. {
  16. private ArtworkModel.Item m_artwork;
  17. private Bitmap m_artBitmap;
  18. private Bitmap m_targetBitmap;
  19. private Bitmap m_overlapBitmap;
  20. public ArtworkUserControl()
  21. {
  22. InitializeComponent();
  23. }
  24. public virtual void Init()
  25. {
  26. }
  27. public virtual bool ArtChanged { get; set; }
  28. public virtual bool AuxiliaryLineEnabled { get; set; }
  29. public virtual bool GridLineEnabled { get; set; }
  30. public virtual int Transparency { get; set; }
  31. public virtual int TargetTransparency { get; set; }
  32. public virtual bool GenerateTargetPoint { get; set; }
  33. public ArtworkModel.Item Artwork
  34. {
  35. set { this.m_artwork = value; }
  36. get { return this.m_artwork; }
  37. }
  38. public Bitmap ArtBitmap
  39. {
  40. get
  41. {
  42. return this.m_artBitmap;
  43. }
  44. set
  45. {
  46. this.m_artBitmap = value;
  47. BitmapArray[0] = this.m_artBitmap;
  48. CalcOverlapWidthAndHeight();
  49. Init();
  50. }
  51. }
  52. public Bitmap TargetBitmap
  53. {
  54. get
  55. {
  56. return this.m_targetBitmap;
  57. }
  58. set
  59. {
  60. this.m_targetBitmap = value;
  61. BitmapArray[1] = this.m_targetBitmap;
  62. CalcOverlapWidthAndHeight();
  63. Init();
  64. }
  65. }
  66. public Bitmap TargetBitmapClone { get; set; }
  67. public Bitmap[] BitmapArray { get; } = new Bitmap[2];
  68. public int OverlapWidth { get; set; }
  69. public int OverlapHeight { get; set; }
  70. /// <summary>
  71. /// 工艺图和目标图拼合后的图片
  72. /// </summary>
  73. public Bitmap OverlapBitmap
  74. {
  75. get
  76. {
  77. return this.m_overlapBitmap;
  78. }
  79. set
  80. {
  81. this.m_overlapBitmap = value;
  82. Init();
  83. }
  84. }
  85. /// <summary>
  86. /// 目标图移动的相对y坐标
  87. /// </summary>
  88. public int TargetTop { get; set; } = 0;
  89. /// <summary>
  90. /// 目标图移动的相对x坐标
  91. /// </summary>
  92. public int TargetLeft { get; set; } = 0;
  93. private void CalcOverlapWidthAndHeight()
  94. {
  95. OverlapWidth = 0;
  96. OverlapHeight = 0;
  97. if (ArtBitmap != null)
  98. {
  99. OverlapWidth = ArtBitmap.Width;
  100. OverlapHeight = ArtBitmap.Height;
  101. }
  102. if (TargetBitmap != null)
  103. {
  104. OverlapWidth = OverlapWidth > TargetBitmap.Width ? OverlapWidth : TargetBitmap.Width;
  105. OverlapHeight = OverlapHeight > TargetBitmap.Height ? OverlapHeight : TargetBitmap.Height;
  106. }
  107. }
  108. public enum OverlapType
  109. {
  110. CENTER = 0, //中间重叠
  111. TOPLEFT = 1, //左上角
  112. }
  113. public Bitmap Overlap(OverlapType type)
  114. {
  115. Bitmap[] bitmaps = this.BitmapArray;
  116. int width = this.OverlapWidth;
  117. int height = this.OverlapHeight;
  118. if (width == 0 || height == 0) {
  119. return null;
  120. }
  121. Bitmap bmPhoto = new Bitmap(width, height);
  122. float x = 0;
  123. float y = 0;
  124. //load the Bitmap into a Graphics object
  125. Graphics grPhoto = Graphics.FromImage(bmPhoto);
  126. //Set the rendering quality for this Graphics object
  127. grPhoto.SmoothingMode = SmoothingMode.AntiAlias;//清除锯齿的呈现
  128. //haix
  129. for (int i = 0; i < bitmaps.Length; i++)
  130. {
  131. Bitmap img = bitmaps[i];
  132. if(img == null)
  133. {
  134. continue;
  135. }
  136. if (type == OverlapType.CENTER)
  137. {
  138. x = (width - img.Width) / 2.0f;
  139. y = (height - img.Height) / 2.0f;
  140. if (i == 1)
  141. {
  142. x += TargetLeft;
  143. y += TargetTop;
  144. }
  145. }
  146. grPhoto.DrawImage(img, new PointF(x,y));
  147. }
  148. return bmPhoto;
  149. }
  150. //定义图像透明度调整函数
  151. public Bitmap MakeTransparent(Bitmap src, int num)
  152. {
  153. return ImageTools.MakeTransparent(src, num);
  154. }
  155. }
  156. }