ImageItemSetting.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. 
  2. using System;
  3. using System.Drawing;
  4. namespace Dongke.WinForm.Controls.InvoiceLayout
  5. {
  6. internal partial class ImageItemSetting : Setting
  7. {
  8. #region 成员变量
  9. private string _fileName = string.Empty; // 图片文件名
  10. private Image _image; // 图片
  11. private decimal _imageWidth = 1; // 图片的宽
  12. private decimal _imageHeight = 1; // 图片的高
  13. private decimal _imageSizeRatio = 1; // 图片的长宽比
  14. private bool _isImageSizeChanging = false; // 图片尺寸改变
  15. private bool _sideRatioFixed = true; // 图片的长宽比固定
  16. #endregion 成员变量
  17. #region 属性
  18. /// <summary>
  19. /// 图片X(mm)
  20. /// </summary>
  21. public float ImageLocationX
  22. {
  23. get
  24. {
  25. return System.Convert.ToSingle(numLoctionX.Value);
  26. }
  27. set
  28. {
  29. decimal d = System.Convert.ToDecimal(value);
  30. if (d < numLoctionX.Minimum)
  31. {
  32. d = numLoctionX.Minimum;
  33. }
  34. else if (numLoctionX.Maximum < d)
  35. {
  36. d = numLoctionX.Maximum;
  37. }
  38. numLoctionX.Value = d;
  39. }
  40. }
  41. /// <summary>
  42. /// 图片Y(mm)
  43. /// </summary>
  44. public float ImageLocationY
  45. {
  46. get
  47. {
  48. return System.Convert.ToSingle(numLoctionY.Value);
  49. }
  50. set
  51. {
  52. decimal d = System.Convert.ToDecimal(value);
  53. if (d < numLoctionY.Minimum)
  54. {
  55. d = numLoctionY.Minimum;
  56. }
  57. else if (numLoctionY.Maximum < d)
  58. {
  59. d = numLoctionY.Maximum;
  60. }
  61. numLoctionY.Value = d;
  62. }
  63. }
  64. /// <summary>
  65. /// 图片的宽(mm)
  66. /// </summary>
  67. public float ImageWidth
  68. {
  69. get
  70. {
  71. return System.Convert.ToSingle(numWidth.Value);
  72. }
  73. set
  74. {
  75. decimal d = System.Convert.ToDecimal(value);
  76. if (d < numWidth.Minimum)
  77. {
  78. d = numWidth.Minimum;
  79. }
  80. else if (numWidth.Maximum < d)
  81. {
  82. d = numWidth.Maximum;
  83. }
  84. numWidth.Value = d;
  85. }
  86. }
  87. /// <summary>
  88. /// 图片的高(mm)
  89. /// </summary>
  90. public float ImageHeight
  91. {
  92. get
  93. {
  94. return System.Convert.ToSingle(numHeight.Value);
  95. }
  96. set
  97. {
  98. decimal d = System.Convert.ToDecimal(value);
  99. if (d < numHeight.Minimum)
  100. {
  101. d = numHeight.Minimum;
  102. }
  103. else if (numHeight.Maximum < d)
  104. {
  105. d = numHeight.Maximum;
  106. }
  107. numHeight.Value = d;
  108. }
  109. }
  110. /// <summary>
  111. /// 图片的长宽比固定
  112. /// </summary>
  113. public bool SideRatioFixed
  114. {
  115. get
  116. {
  117. return chkFixedRatio.Checked;
  118. }
  119. set
  120. {
  121. _sideRatioFixed = value;
  122. }
  123. }
  124. /// <summary>
  125. /// 图片名
  126. /// </summary>
  127. public string FileName
  128. {
  129. get
  130. {
  131. return _fileName;
  132. }
  133. set
  134. {
  135. _fileName = value;
  136. }
  137. }
  138. /// <summary>
  139. /// 图片
  140. /// </summary>
  141. public Image Image
  142. {
  143. get
  144. {
  145. return _image;
  146. }
  147. set
  148. {
  149. _image = value;
  150. }
  151. }
  152. #endregion 属性
  153. #region 构造函数
  154. /// <summary>
  155. /// 构造函数
  156. /// </summary>
  157. public ImageItemSetting()
  158. {
  159. InitializeComponent();
  160. string directory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
  161. saveFileDialog.Title = Text;
  162. saveFileDialog.FileName = _fileName;
  163. saveFileDialog.Filter = LayoutConsts.FILE_DIALOG_IMAGE_FILTER;
  164. saveFileDialog.FilterIndex = 6;
  165. if (System.IO.Directory.Exists(directory))
  166. {
  167. saveFileDialog.InitialDirectory = directory;
  168. }
  169. else
  170. {
  171. saveFileDialog.InitialDirectory =
  172. Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
  173. }
  174. }
  175. #endregion 构造函数
  176. #region 事件处理
  177. /// <summary>
  178. /// Shown
  179. /// </summary>
  180. /// <param name="sender">指定的对象</param>
  181. /// <param name="e">提供的事件数据</param>
  182. private void ImageItemSetting_Shown(object sender, System.EventArgs e)
  183. {
  184. chkFixedRatio.Checked = _sideRatioFixed;
  185. }
  186. /// <summary>
  187. /// 宽改变
  188. /// </summary>
  189. /// <param name="sender">指定的对象</param>
  190. /// <param name="e">提供的事件数据</param>
  191. private void numWidth_ValueChanged(object sender, System.EventArgs e)
  192. {
  193. if (!_isImageSizeChanging)
  194. {
  195. if (chkFixedRatio.Checked)
  196. {
  197. _isImageSizeChanging = true;
  198. _imageHeight = LayoutCommon.Truncate(numWidth.Value * _imageSizeRatio,
  199. numWidth.DecimalPlaces);
  200. if (_imageHeight < numHeight.Minimum
  201. || numHeight.Maximum < _imageHeight)
  202. {
  203. _imageHeight = numHeight.Value;
  204. numWidth.Value = _imageWidth;
  205. }
  206. else
  207. {
  208. _imageWidth = numWidth.Value;
  209. numHeight.Value = _imageHeight;
  210. }
  211. _isImageSizeChanging = false;
  212. }
  213. else
  214. {
  215. _imageWidth = numWidth.Value;
  216. }
  217. }
  218. }
  219. /// <summary>
  220. /// 高改变
  221. /// </summary>
  222. /// <param name="sender">指定的对象</param>
  223. /// <param name="e">提供的事件数据</param>
  224. private void numHeight_ValueChanged(object sender, System.EventArgs e)
  225. {
  226. if (!_isImageSizeChanging)
  227. {
  228. if (chkFixedRatio.Checked)
  229. {
  230. _isImageSizeChanging = true;
  231. _imageWidth = LayoutCommon.Truncate(numHeight.Value / _imageSizeRatio,
  232. numHeight.DecimalPlaces);
  233. if (_imageWidth < numWidth.Minimum
  234. || numWidth.Maximum < _imageWidth)
  235. {
  236. _imageWidth = numWidth.Value;
  237. numHeight.Value = _imageHeight;
  238. }
  239. else
  240. {
  241. numWidth.Value = _imageWidth;
  242. _imageHeight = numHeight.Value;
  243. }
  244. _isImageSizeChanging = false;
  245. }
  246. else
  247. {
  248. _imageHeight = numHeight.Value;
  249. }
  250. }
  251. }
  252. /// <summary>
  253. /// 长宽比固定
  254. /// </summary>
  255. /// <param name="sender">指定的对象</param>
  256. /// <param name="e">提供的事件数据</param>
  257. private void chkFixedRatio_CheckedChanged(object sender, System.EventArgs e)
  258. {
  259. if (chkFixedRatio.Checked)
  260. {
  261. _imageSizeRatio = _imageHeight / _imageWidth;
  262. }
  263. }
  264. /// <summary>
  265. /// 图片另存为
  266. /// </summary>
  267. /// <param name="sender">指定的对象</param>
  268. /// <param name="e">提供的事件数据</param>
  269. private void btnSaveAs_Click(object sender, EventArgs e)
  270. {
  271. if (_image == null)
  272. {
  273. return;
  274. }
  275. saveFileDialog.FileName = _fileName;
  276. if (saveFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  277. {
  278. using (Image image = (Image)_image.Clone())
  279. {
  280. image.Save(saveFileDialog.FileName);
  281. }
  282. }
  283. }
  284. #endregion 事件处理
  285. }
  286. }