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 属性 /// /// 图片X(mm) /// 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; } } /// /// 图片Y(mm) /// 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; } } /// /// 图片的宽(mm) /// 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; } } /// /// 图片的高(mm) /// 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; } } /// /// 图片的长宽比固定 /// public bool SideRatioFixed { get { return chkFixedRatio.Checked; } set { _sideRatioFixed = value; } } /// /// 图片名 /// public string FileName { get { return _fileName; } set { _fileName = value; } } /// /// 图片 /// public Image Image { get { return _image; } set { _image = value; } } #endregion 属性 #region 构造函数 /// /// 构造函数 /// 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 事件处理 /// /// Shown /// /// 指定的对象 /// 提供的事件数据 private void ImageItemSetting_Shown(object sender, System.EventArgs e) { chkFixedRatio.Checked = _sideRatioFixed; } /// /// 宽改变 /// /// 指定的对象 /// 提供的事件数据 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; } } } /// /// 高改变 /// /// 指定的对象 /// 提供的事件数据 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; } } } /// /// 长宽比固定 /// /// 指定的对象 /// 提供的事件数据 private void chkFixedRatio_CheckedChanged(object sender, System.EventArgs e) { if (chkFixedRatio.Checked) { _imageSizeRatio = _imageHeight / _imageWidth; } } /// /// 图片另存为 /// /// 指定的对象 /// 提供的事件数据 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 事件处理 } }