winform中实现图片左旋,右旋

左旋

 

继续旋转

右旋:

继续右旋

效果展示结束:

下面讲讲利用什么来实现的:

由于不光判断是否是图片的*.BMP;*.JPG;*.GIF;*.TIF;*.PNG 还有*.PDF文件,因此写了个自定义控件里面处理左旋,右旋,背景颜色,之类的。这里不来实现PDF

ImageList.cs里面的处理:

private List<PreviewControl> poPreviews = null;
		public ImageList()
		{
			InitializeComponent();
			poPreviews = new List<PreviewControl>();
		}
		/// <summary>
		/// 取得选择的图片
		/// </summary>
		/// <returns></returns>
		private PreviewControl GetCurrentPreview()
		{
			for (int i = 0; i < poPreviews.Count; i++)
			{
				if (poPreviews[i].Selected)
				{
					return poPreviews[i];
				}
			}
			return null;
		}

		/// <summary>
		/// 左旋
		/// </summary>
		/// <param name="eventSender"></param>
		/// <param name="eventArgs"></param>
		public void cmdLeftRoteImage_Click(System.Object eventSender, System.EventArgs eventArgs)
		{
			PreviewControl oCurrent = GetCurrentPreview();
			if (oCurrent != null)
			{
				oCurrent.RotateLeft();
			}
		}

		/// <summary>
		/// 右旋
		/// </summary>
		/// <param name="eventSender"></param>
		/// <param name="eventArgs"></param>
		public void cmdRightRoteImage_Click(System.Object eventSender, System.EventArgs eventArgs)
		{
			PreviewControl oCurrent = GetCurrentPreview();
			if (oCurrent != null)
			{
				oCurrent.RotateRight();
			}
		}

PreviewControll.cs 的代码: 自定义控件上面拖放PictureBox 和WebBrowser

 [DllImport("user32.dll")]
        extern static short GetKeyState(int vKey);

        /// <summary>
        /// 加载图片
        /// </summary>
        private string psImageLocation = String.Empty;
        /// <summary>
	/// 左旋,右旋后直接保存
        /// false:不保存、  true:保存
        /// </summary>
        private bool pbRotateSaveFlg = true;

        /// <summary>
        /// PDF文件类型的标志
        /// TRUE:PDF文件  FALSE:不是PDF文件
        /// </summary>
        private bool pbPdfFlg = false;

        /// <summary>
        /// 选择时的颜色
        /// </summary>
        private Color poSelectColor = SystemColors.Highlight;

        /// <summary>
        /// 未选择的颜色
        /// </summary>
        private Color poUnSelectColor = SystemColors.Control;

        /// <summary>
        /// 图片左旋右旋的值
        /// </summary>
        private int piRoteSum = 0;

        /// <summary>
        /// 图片预览控件初始化
        /// </summary>
        public PreviewControl()
        {
            InitializeComponent();                      
        }

        private bool IsControlKeyDown()
        {
            return (GetKeyState(0x0011) & 0x0000FF00) != 0;
        }

        private bool IsShiftKeyDown()
        {
            return (GetKeyState(0x0010) & 0x0000FF00) != 0;
        }

        /// <summary>
        /// 加载的图片
        /// </summary>
        public string ImageLocation
        {
            set
            {
                psImageLocation = value;
                if (psImageLocation.Length == 0)
                {
                    pictureBox1.Visible = false;
                    webBrowser1.Visible = false;
                }
                else if (value.EndsWith(".pdf", StringComparison.CurrentCultureIgnoreCase))
                {
                    webBrowser1.Visible = true;
                    webBrowser1.Navigate(value);
                    pictureBox2.Tag = value;
                    pictureBox2.Visible = true;
                    pictureBox1.Visible = false;
                    pbPdfFlg = true;
                }
                else
                {
                    webBrowser1.Visible = false;
                    pictureBox1.Visible = true;
                    pictureBox1.ImageLocation = value;
                }
            }
            get
            {
                return psImageLocation;
            }
        }

        /// <summary>
        /// 选择时的颜色
        /// </summary>
        public Color SelectColor
        {
            get
            {
                return poSelectColor;
            }
            set
            {
                poSelectColor = value;
            }
        }

        /// <summary>
        /// 没有选择的颜色
        /// </summary>
        public Color UnSelectColor
        {
            get
            {
                return poUnSelectColor;
            }
            set
            {
                poUnSelectColor = value;
            }
        }
        /// <summary>
        /// 图片旋转的值入 90 180 270 度
        /// </summary>
        public int RotateSum
        {
            get
            {
                return piRoteSum;
            }
            set
            {
                piRoteSum = value;
            }
        }


        /// <summary>
        /// 保存旋转后位置的状态
        /// </summary>
        public bool RotateSaveFlg
        {
            set
            {
                pbRotateSaveFlg = value;
            }
        }

        /// <summary>
        /// 左旋
        /// </summary>
        public void RotateLeft()
        {
            if (pictureBox1.Image != null)
            {
                //扩展名判断
                if (CheckFileExtension(this.Tag.ToString()) == GFBEFUNC.FileExtension.PDF)
                {
                    return;
                }
                Image oSrc = pictureBox1.Image;
                Bitmap oDst = new Bitmap(oSrc);
                oDst.RotateFlip(RotateFlipType.Rotate270FlipNone);
                pictureBox1.Image = oDst;
                piRoteSum = piRoteSum + 270;
                if (!pbRotateSaveFlg)
                {
                    return;
                }
                ImageSave((Bitmap)pictureBox1.Image, 100, pictureBox1.ImageLocation,0,0);
            }
        }
        /// <summary>
        /// 右旋
        /// </summary>
        public void RotateRight()
        {
            if (pictureBox1.Image != null)
            {
				//扩展名判断
                if (CheckFileExtension(this.Tag.ToString()) == GFBEFUNC.FileExtension.PDF)
                {
                    return;
                }
                Image oSrc = pictureBox1.Image;
                Bitmap oDst = new Bitmap(oSrc);
                oDst.RotateFlip(RotateFlipType.Rotate90FlipNone);
                pictureBox1.Image = oDst;
                piRoteSum = piRoteSum + 90;
                if (!pbRotateSaveFlg)
                {
                    return;
                }
                ImageSave((Bitmap)pictureBox1.Image, 100, pictureBox1.ImageLocation,0,0);
            }
        }


        /// <summary>
        /// 清除图片的记忆位置
        /// </summary>
        public void ClearImageMemory()
        {
            if (pictureBox1.Image != null)
            {
                pictureBox1.Image.Dispose();
            }
            if (pictureBox2.Image != null)
            {
                pictureBox2.Image.Dispose();
            }
            if (!webBrowser1.IsDisposed)
            {
                webBrowser1.Dispose();
            }
        }

        /// <summary>
        /// 利用GC回收释放内存
        /// </summary>
        public void GCCollect()
        {
            pictureBox1.Image = null;
            pictureBox2.Image = null;
            webBrowser1 = null;
        }
  /// <summary> /// 保存图片  /// <param name="src">图片地址</param>
  /// <param name="scale">比例</param>
  /// <param name="asSaveFile"></param>
  /// <param name="aiWidth">宽度</param>
  /// <param name="aiHeight">高度</param>
  public static void ImageSave(Bitmap src, int scale, string asSaveFile, int aiWidth, int aiHeight)
  {
   int w = src.Width;
   int h = src.Height;
   if (scale < 100)
   {
    w = aiWidth;
    h = aiHeight;
   }
   Bitmap dest = new Bitmap(w, h);
   Graphics g = Graphics.FromImage(dest);
   g.SmoothingMode = SmoothingMode.HighQuality;
   g.InterpolationMode = InterpolationMode.High;
   g.DrawImage(src, 0, 0, w, h);
   g.Dispose();
   dest.Save(asSaveFile, ImageFormat.Jpeg);
   dest.Dispose();
  }
public static FileExtension CheckFileExtension(string fileName)
		{
			if (!File.Exists(fileName))
			{
				return FileExtension.VALIDFILE;
			}
			FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
			System.IO.BinaryReader br = new System.IO.BinaryReader(fs);
			string fileType = string.Empty;
			FileExtension extension = FileExtension.VALIDFILE;
			try
			{
				byte data = br.ReadByte();
				fileType += data.ToString();
				data = br.ReadByte();
				fileType += data.ToString();
				extension = (FileExtension)Enum.Parse(typeof(FileExtension), fileType);
				if (extension.ToString().Equals(fileType))
				{
					extension = FileExtension.VALIDFILE;
				}
			}
			catch
			{
				extension = FileExtension.VALIDFILE;
			}
			finally
			{
				if (fs != null)
				{
					fs.Close();
					br.Close();
				}
			}

			return extension;
		}
		public enum FileExtension
		{
			JPEG = 255216,
			TIF = 7373,
			GIF = 7173,
			BMP = 6677,
			PNG = 13780,
			PDF = 3780,
			VALIDFILE = 9999999
		}


贴出来的核心代码:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 在WinForm实现圆形进度条,可以使用ProgressBar控件,并通过自定义绘制来实现。 首先,在WinForm的设计界面上添加一个ProgressBar控件,并设置其Style为Continuous,以实现平滑的动画效果。 接下来,在Form的Load事件添加以下代码: ```csharp private void Form1_Load(object sender, EventArgs e) { progressBar1.Maximum = 100; // 设置进度条的最大值 progressBar1.Minimum = 0; // 设置进度条的最小值 progressBar1.Step = 1; // 设置进度条每次增加的步长 // 设置进度条的样式为自定义 progressBar1.SetStyle(ControlStyles.UserPaint, true); progressBar1.SetStyle(ControlStyles.OptimizedDoubleBuffer, true); progressBar1.SetStyle(ControlStyles.AllPaintingInWmPaint, true); progressBar1.SetStyle(ControlStyles.SupportsTransparentBackColor, true); } ``` 然后,对ProgressBar的绘制事件进行自定义绘制,以实现圆形进度条的效果。在Form添加以下代码: ```csharp private void progressBar1_Paint(object sender, PaintEventArgs e) { using (Graphics graphics = e.Graphics) { graphics.SmoothingMode = SmoothingMode.AntiAlias; graphics.Clear(progressBar1.BackColor); float angle = 360 * progressBar1.Value / (progressBar1.Maximum - progressBar1.Minimum); using (SolidBrush brush = new SolidBrush(progressBar1.ForeColor)) { graphics.FillPie(brush, new Rectangle(0, 0, progressBar1.Width - 1, progressBar1.Height - 1), -90, angle); } } } ``` 最后,通过调用ProgressBar的PerformStep方法来动态更新进度条的进度,即可实现圆形进度条的效果。例如,在按钮的Click事件添加以下代码: ```csharp private void button1_Click(object sender, EventArgs e) { if (progressBar1.Value < progressBar1.Maximum) { progressBar1.PerformStep(); } } ``` 通过以上步骤,就可以在WinForm实现圆形进度条的效果了。 ### 回答2: 在WinForm实现圆形进度条可以通过自定义控件来实现。 首先,创建一个继承自Control类的自定义控件CircleProgressBar,然后在该类重写OnPaint方法来绘制圆形进度条。 在OnPaint方法,我们可以使用Graphics类的一些方法和属性来绘制圆形进度条的背景和进度。具体实现步骤如下: 1. 创建一个Graphics对象,用于绘制进度条。 2. 设置Graphics对象的SmoothingMode属性为AntiAlias,以获得平滑的绘制效果。 3. 根据控件的宽度和高度计算出圆形的半径。 4. 创建一个矩形,作为进度条的外框。 5. 使用Graphics对象的DrawEllipse方法绘制圆形的外框。 6. 设置Graphics对象的Clip属性为矩形,以便限制进度条的绘制范围。 7. 计算出进度条的角度,根据进度值和总进度值的比例计算。 8. 使用Graphics对象的DrawArc方法绘制进度条的弧度。 9. 调用Graphics对象的Dispose方法释放资源。 在使用CircleProgressBar控件时,只需将其添加到窗体,并设置进度值和总进度值即可。 示例代码如下: ```csharp using System; using System.Drawing; using System.Windows.Forms; public class CircleProgressBar : Control { private int progress; private int total; public CircleProgressBar() { progress = 0; total = 100; } public int Progress { get { return progress; } set { progress = value; if (progress < 0) progress = 0; if (progress > total) progress = total; Invalidate(); // 重绘控件 } } public int Total { get { return total; } set { total = value; if (total <= 0) total = 1; if (progress > total) progress = total; Invalidate(); // 重绘控件 } } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); Graphics g = e.Graphics; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; int diameter = Math.Min(Width, Height); // 获取圆形的直径 Rectangle rect = new Rectangle(0, 0, diameter, diameter); g.DrawEllipse(Pens.Black, rect); // 绘制圆形的外框 using (GraphicsPath path = new GraphicsPath()) using (Pen pen = new Pen(Color.Blue, 5)) { path.AddArc(rect, -90, (float)(progress * 360 / total)); // 计算进度条的弧度 g.Clip = new Region(rect); // 设置绘制范围 g.DrawPath(pen, path); // 绘制进度条的弧度 } g.Dispose(); } } ``` 通过以上步骤,我们就可以自定义一个圆形进度条的WinForm控件,并在窗体使用它来展示圆形的进度。 ### 回答3: 在WinForm实现圆形进度条,可以通过自定义控件来实现。以下是实现的步骤: 1. 创建一个新的自定义控件,继承自Panel或者UserControl,并命名为CircularProgressBar。 2. 在该自定义控件,声明一个整型变量progressValue用于表示进度的值,以及一个整型变量maxValue表示进度的最大值。 3. 在构造函数,设置控件的默认大小和背景颜色。 4. 重写OnPaint方法,在该方法绘制圆形进度条的背景和进度条的进度。 5. 在OnPaint方法,先绘制圆形背景,可以使用Graphics的DrawEllipse方法来绘制一个圆形。 6. 根据当前的进度值和最大值,计算出进度条的角度,通过遍历的方式,使用Graphics的DrawArc方法来绘制进度条。 7. 在控件新增一个SetProgress方法,用于设置进度条的进度值,并在该方法调用Invalidate方法触发控件的重绘。 8. 在MainForm使用该自定义控件,可以通过设置CircularProgressBar的Size和Location属性来调整控件的大小和位置。 使用以上的步骤,即可在WinForm实现一个圆形进度条的自定义控件。控件的进度值可以通过SetProgress方法来动态设置,从而实现进度的更新和显示。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值