SVGEditorAdvancedForm.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. using Svg;
  2. using System;
  3. using System.Drawing;
  4. using System.Drawing.Drawing2D;
  5. using System.Drawing.Imaging;
  6. using System.Windows.Forms;
  7. using FastReport.SVG;
  8. using FastReport.Utils;
  9. namespace FastReport.Forms
  10. {
  11. internal partial class SVGEditorAdvancedForm : BaseForm
  12. {
  13. private SVGObject svgObject;
  14. private SVGObject originalSvgObject;
  15. private string originalSvgString;
  16. public SVGObject SvgObject
  17. {
  18. get { return svgObject; }
  19. set { svgObject = value; }
  20. }
  21. private void Init()
  22. {
  23. originalSvgObject = (SVGObject)svgObject.Clone();
  24. originalSvgString = svgObject.SVGString;
  25. pictureBox.Image = Update(pictureBox.Width, pictureBox.Height);
  26. Reset();
  27. NUpDownMinX.ValueChanged += SetSvgObject;
  28. NUpDownMinY.ValueChanged += SetSvgObject;
  29. NUpDownWidth.ValueChanged += SetSvgObject;
  30. NUpDownHeight.ValueChanged += SetSvgObject;
  31. cbAspectRatio.CheckedChanged += SetSvgObject;
  32. rbGrayscale.CheckedChanged += SetSvgObject;
  33. rbNone.CheckedChanged += SetSvgObject;
  34. SizeChanged += delegate (object s, EventArgs e)
  35. {
  36. CenterImage();
  37. };
  38. }
  39. public override void UpdateDpiDependencies()
  40. {
  41. base.UpdateDpiDependencies();
  42. MinimumSize = this.LogicalToDevice(new Size(595, 477));
  43. }
  44. public SVGEditorAdvancedForm(SVGObject svgObject)
  45. {
  46. this.svgObject = svgObject;
  47. InitializeComponent();
  48. Init();
  49. Localize();
  50. UIUtils.CheckRTL(this);
  51. UpdateDpiDependencies();
  52. }
  53. #region Init & Reset
  54. private void ResetSvgObjectToOriginal()
  55. {
  56. svgObject = (SVGObject)originalSvgObject.Clone();
  57. if (!string.IsNullOrEmpty(originalSvgString))
  58. svgObject.SVGString = originalSvgString;
  59. if (svgObject.SvgDocument.AspectRatio.Align != SvgPreserveAspectRatio.none)
  60. svgObject.AspectRatio = new SvgAspectRatio(SvgPreserveAspectRatio.xMidYMid);
  61. }
  62. private void Localize()
  63. {
  64. MyRes res = new MyRes("Forms,PictureEditorAdvanced");
  65. Text = res.Get("");
  66. btnOK.Text = Res.Get("Buttons,Ok");
  67. btnCancel.Text = Res.Get("Buttons,Cancel");
  68. btnReset.Text = res.Get("Reset");
  69. gbColor.Text = res.Get("Color");
  70. lblMinX.Text = res.Get("MinX");
  71. lblMinY.Text = res.Get("MinY");
  72. lblWidth.Text = res.Get("Horizontal");
  73. lblHeight.Text = res.Get("Vertical");
  74. cbAspectRatio.Text = res.Get("AspectRatio");
  75. rbNone.Text = Res.Get("Misc,None");
  76. rbGrayscale.Text = res.Get("Grayscale");
  77. }
  78. private void Reset()
  79. {
  80. ResetSvgObjectToOriginal();
  81. ResetAspectRatio();
  82. ResetViewBox();
  83. ResetGrayScale();
  84. }
  85. private void ResetAspectRatio()
  86. {
  87. cbAspectRatio.Checked = (svgObject.SvgDocument.AspectRatio.Align == SvgPreserveAspectRatio.none) ? false : true;
  88. }
  89. private void ResetViewBox()
  90. {
  91. NUpDownMinX.Minimum = 0;
  92. NUpDownMinX.Maximum = decimal.MaxValue;
  93. NUpDownMinX.Value = (decimal)svgObject.SvgDocument.ViewBox.MinX;
  94. NUpDownMinY.Minimum = 0;
  95. NUpDownMinY.Maximum = decimal.MaxValue;
  96. NUpDownMinY.Value = (decimal)svgObject.SvgDocument.ViewBox.MinY;
  97. NUpDownWidth.Minimum = 0;
  98. NUpDownWidth.Maximum = decimal.MaxValue;
  99. NUpDownWidth.Value = (decimal)svgObject.SvgDocument.ViewBox.Width;
  100. NUpDownHeight.Minimum = 0;
  101. NUpDownHeight.Maximum = decimal.MaxValue;
  102. NUpDownHeight.Value = (decimal)svgObject.SvgDocument.ViewBox.Height;
  103. }
  104. private void ResetGrayScale()
  105. {
  106. svgObject.Grayscale = originalSvgObject.Grayscale;
  107. if (svgObject.Grayscale)
  108. rbGrayscale.Checked = true;
  109. else
  110. rbNone.Checked = true;
  111. }
  112. #endregion
  113. private void SetSvgObject(object sender, EventArgs e)
  114. {
  115. if (cbAspectRatio.Checked)
  116. svgObject.AspectRatio = new SvgAspectRatio(SvgPreserveAspectRatio.xMidYMid);
  117. else
  118. {
  119. svgObject.SizeMode = PictureBoxSizeMode.StretchImage;
  120. svgObject.AspectRatio = new SvgAspectRatio(SvgPreserveAspectRatio.none);
  121. }
  122. svgObject.Grayscale = rbGrayscale.Checked ? true : false;
  123. if (sender == NUpDownMinX)
  124. {
  125. svgObject.ViewBox = new SvgViewBox((float)NUpDownMinX.Value,
  126. svgObject.SvgDocument.ViewBox.MinY,
  127. svgObject.SvgDocument.ViewBox.Width,
  128. svgObject.SvgDocument.ViewBox.Height);
  129. }
  130. else if (sender == NUpDownMinY)
  131. {
  132. svgObject.ViewBox = new SvgViewBox(svgObject.SvgDocument.ViewBox.MinX,
  133. (float)NUpDownMinY.Value,
  134. svgObject.SvgDocument.ViewBox.Width,
  135. svgObject.SvgDocument.ViewBox.Height);
  136. }
  137. else if (sender == NUpDownWidth)
  138. {
  139. svgObject.ViewBox = new SvgViewBox(svgObject.SvgDocument.ViewBox.MinX,
  140. svgObject.SvgDocument.ViewBox.MinY,
  141. (float)NUpDownWidth.Value,
  142. svgObject.SvgDocument.ViewBox.Height);
  143. }
  144. else if (sender == NUpDownHeight)
  145. {
  146. svgObject.ViewBox = new SvgViewBox(svgObject.SvgDocument.ViewBox.MinX,
  147. svgObject.SvgDocument.ViewBox.MinY,
  148. svgObject.SvgDocument.ViewBox.Width,
  149. (float)NUpDownHeight.Value);
  150. }
  151. Redraw();
  152. }
  153. #region Main & Etc
  154. private void Redraw()
  155. {
  156. pictureBox.Image = Update(pictureBox.Width, pictureBox.Height);
  157. CenterImage();
  158. }
  159. private void CenterImage()
  160. {
  161. pictureBox.Left = Math.Max(0, (pictureBox.Parent.Width - pictureBox.Width) / 2);
  162. pictureBox.Top = Math.Max(0, (pictureBox.Parent.Height - pictureBox.Height) / 2);
  163. }
  164. private Image Update(int width, int height)
  165. {
  166. Image image;
  167. if (svgObject.Grayscale)
  168. {
  169. if (svgObject.SVGGrayscale == null)
  170. svgObject.GetSVGGrayscale();
  171. image = svgObject.SVGGrayscale.Draw(width, height);
  172. }
  173. else
  174. image = svgObject.SvgDocument.Draw(width, height);
  175. Rectangle destRect = new Rectangle(0, 0, width, height);
  176. Bitmap destImage = new Bitmap(width, height);
  177. destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);
  178. using (Graphics graphics = Graphics.FromImage(destImage))
  179. {
  180. graphics.CompositingMode = CompositingMode.SourceCopy;
  181. graphics.CompositingQuality = CompositingQuality.HighQuality;
  182. graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
  183. graphics.SmoothingMode = SmoothingMode.HighQuality;
  184. graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
  185. using (ImageAttributes wrapMode = new ImageAttributes())
  186. {
  187. wrapMode.SetWrapMode(WrapMode.TileFlipXY);
  188. graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
  189. }
  190. }
  191. return destImage;
  192. }
  193. private void btnReset_Click(object sender, EventArgs e)
  194. {
  195. Reset();
  196. Redraw();
  197. }
  198. #endregion
  199. }
  200. }