| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301 |
-
- using System;
- using System.Drawing;
- namespace Dongke.WinForm.Controls.InvoiceLayout
- {
- internal partial class ImageItemSetting : Setting
- {
- #region 成员变量
- private string _fileName = string.Empty; // 图片文件名
- private Image _image; // 图片
- private decimal _imageWidth = 1; // 图片的宽
- private decimal _imageHeight = 1; // 图片的高
- private decimal _imageSizeRatio = 1; // 图片的长宽比
- private bool _isImageSizeChanging = false; // 图片尺寸改变
- private bool _sideRatioFixed = true; // 图片的长宽比固定
- #endregion 成员变量
- #region 属性
- /// <summary>
- /// 图片X(mm)
- /// </summary>
- public float ImageLocationX
- {
- get
- {
- return System.Convert.ToSingle(numLoctionX.Value);
- }
- set
- {
- decimal d = System.Convert.ToDecimal(value);
- if (d < numLoctionX.Minimum)
- {
- d = numLoctionX.Minimum;
- }
- else if (numLoctionX.Maximum < d)
- {
- d = numLoctionX.Maximum;
- }
- numLoctionX.Value = d;
- }
- }
- /// <summary>
- /// 图片Y(mm)
- /// </summary>
- public float ImageLocationY
- {
- get
- {
- return System.Convert.ToSingle(numLoctionY.Value);
- }
- set
- {
- decimal d = System.Convert.ToDecimal(value);
- if (d < numLoctionY.Minimum)
- {
- d = numLoctionY.Minimum;
- }
- else if (numLoctionY.Maximum < d)
- {
- d = numLoctionY.Maximum;
- }
- numLoctionY.Value = d;
- }
- }
- /// <summary>
- /// 图片的宽(mm)
- /// </summary>
- public float ImageWidth
- {
- get
- {
- return System.Convert.ToSingle(numWidth.Value);
- }
- set
- {
- decimal d = System.Convert.ToDecimal(value);
- if (d < numWidth.Minimum)
- {
- d = numWidth.Minimum;
- }
- else if (numWidth.Maximum < d)
- {
- d = numWidth.Maximum;
- }
- numWidth.Value = d;
- }
- }
- /// <summary>
- /// 图片的高(mm)
- /// </summary>
- public float ImageHeight
- {
- get
- {
- return System.Convert.ToSingle(numHeight.Value);
- }
- set
- {
- decimal d = System.Convert.ToDecimal(value);
- if (d < numHeight.Minimum)
- {
- d = numHeight.Minimum;
- }
- else if (numHeight.Maximum < d)
- {
- d = numHeight.Maximum;
- }
- numHeight.Value = d;
- }
- }
- /// <summary>
- /// 图片的长宽比固定
- /// </summary>
- public bool SideRatioFixed
- {
- get
- {
- return chkFixedRatio.Checked;
- }
- set
- {
- _sideRatioFixed = value;
- }
- }
- /// <summary>
- /// 图片名
- /// </summary>
- public string FileName
- {
- get
- {
- return _fileName;
- }
- set
- {
- _fileName = value;
- }
- }
- /// <summary>
- /// 图片
- /// </summary>
- public Image Image
- {
- get
- {
- return _image;
- }
- set
- {
- _image = value;
- }
- }
- #endregion 属性
- #region 构造函数
- /// <summary>
- /// 构造函数
- /// </summary>
- public ImageItemSetting()
- {
- InitializeComponent();
- string directory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
- saveFileDialog.Title = Text;
- saveFileDialog.FileName = _fileName;
- saveFileDialog.Filter = LayoutConsts.FILE_DIALOG_IMAGE_FILTER;
- saveFileDialog.FilterIndex = 6;
- if (System.IO.Directory.Exists(directory))
- {
- saveFileDialog.InitialDirectory = directory;
- }
- else
- {
- saveFileDialog.InitialDirectory =
- Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
- }
- }
- #endregion 构造函数
- #region 事件处理
- /// <summary>
- /// Shown
- /// </summary>
- /// <param name="sender">指定的对象</param>
- /// <param name="e">提供的事件数据</param>
- private void ImageItemSetting_Shown(object sender, System.EventArgs e)
- {
- chkFixedRatio.Checked = _sideRatioFixed;
- }
- /// <summary>
- /// 宽改变
- /// </summary>
- /// <param name="sender">指定的对象</param>
- /// <param name="e">提供的事件数据</param>
- private void numWidth_ValueChanged(object sender, System.EventArgs e)
- {
- if (!_isImageSizeChanging)
- {
- if (chkFixedRatio.Checked)
- {
- _isImageSizeChanging = true;
- _imageHeight = LayoutCommon.Truncate(numWidth.Value * _imageSizeRatio,
- numWidth.DecimalPlaces);
- if (_imageHeight < numHeight.Minimum
- || numHeight.Maximum < _imageHeight)
- {
- _imageHeight = numHeight.Value;
- numWidth.Value = _imageWidth;
- }
- else
- {
- _imageWidth = numWidth.Value;
- numHeight.Value = _imageHeight;
- }
- _isImageSizeChanging = false;
- }
- else
- {
- _imageWidth = numWidth.Value;
- }
- }
- }
- /// <summary>
- /// 高改变
- /// </summary>
- /// <param name="sender">指定的对象</param>
- /// <param name="e">提供的事件数据</param>
- private void numHeight_ValueChanged(object sender, System.EventArgs e)
- {
- if (!_isImageSizeChanging)
- {
- if (chkFixedRatio.Checked)
- {
- _isImageSizeChanging = true;
- _imageWidth = LayoutCommon.Truncate(numHeight.Value / _imageSizeRatio,
- numHeight.DecimalPlaces);
- if (_imageWidth < numWidth.Minimum
- || numWidth.Maximum < _imageWidth)
- {
- _imageWidth = numWidth.Value;
- numHeight.Value = _imageHeight;
- }
- else
- {
- numWidth.Value = _imageWidth;
- _imageHeight = numHeight.Value;
- }
- _isImageSizeChanging = false;
- }
- else
- {
- _imageHeight = numHeight.Value;
- }
- }
- }
- /// <summary>
- /// 长宽比固定
- /// </summary>
- /// <param name="sender">指定的对象</param>
- /// <param name="e">提供的事件数据</param>
- private void chkFixedRatio_CheckedChanged(object sender, System.EventArgs e)
- {
- if (chkFixedRatio.Checked)
- {
- _imageSizeRatio = _imageHeight / _imageWidth;
- }
- }
- /// <summary>
- /// 图片另存为
- /// </summary>
- /// <param name="sender">指定的对象</param>
- /// <param name="e">提供的事件数据</param>
- private void btnSaveAs_Click(object sender, EventArgs e)
- {
- if (_image == null)
- {
- return;
- }
- saveFileDialog.FileName = _fileName;
- if (saveFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
- {
- using (Image image = (Image)_image.Clone())
- {
- image.Save(saveFileDialog.FileName);
- }
- }
- }
- #endregion 事件处理
- }
- }
|