• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
wjshan0808

Learn from yesterday, Live for today, For a better tomorrow.
 ————wjshan0808

博客园    首页    新随笔    联系   管理    订阅  订阅

图片缩放/旋转/平移/设置分辨率

#region 设置分辨率
            using (Graphics g = pictureBox2.CreateGraphics())
            {
                g.Clear(pictureBox2.BackColor);//清空
                Bitmap bmp = new Bitmap(pictureBox1.Image);//创建bitmap对象
                ///设置分辨率
                bmp.SetResolution(500F, 500F);
                //重绘图像
                g.DrawImage(bmp, pictureBox2.ClientRectangle, 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel);
                bmp.Dispose();
            }
            #endregion
#region 缩放
            using (Graphics g = pictureBox2.CreateGraphics())
            {
                g.Clear(pictureBox2.BackColor);//清空
                using (Image image = Image.FromFile("3.jpg"))
                {
                    g.ScaleTransform(0.5F, 0.5F, System.Drawing.Drawing2D.MatrixOrder.Append);
                    int width = Convert.ToInt32(0.5 * image.Width);//缩放宽度
                    int height = Convert.ToInt32(0.5 * image.Height);//缩放高度
                    g.DrawImage(image, this.ClientSize.Width / 2, this.ClientSize.Height / 2, width, height);//绘制图像
                }
            }
            #endregion
 #region 平移
            Image img = pictureBox1.Image = Image.FromFile("3.jpg"); 
            using (Graphics g = pictureBox2.CreateGraphics())
            {
                g.Clear(pictureBox2.BackColor);//清空 
                ///平移 
                g.TranslateTransform(0F, 200F);
                //重绘图像 
                g.DrawImage(img, this.pictureBox1.ClientRectangle, 0, 0, img.Width, img.Height, GraphicsUnit.Pixel);
            }
            #endregion
            Image img = pictureBox1.Image = Image.FromFile("4.bmp");
 img.RotateFlip(RotateFlipType.RotateNoneFlipXY);//旋转

#region 旋转 绕顶点using (Graphics g = pictureBox2.CreateGraphics()) { g.Clear(pictureBox2.BackColor);//清空 //旋转 g.RotateTransform(20F);//旋转 //重绘图像 g.DrawImage(img, this.pictureBox1.ClientRectangle, 0, 0, img.Width, img.Height, GraphicsUnit.Pixel); } #endregion

#region 绕图片中心点旋转 using (Graphics g = pictureBox2.CreateGraphics())//创建画板 { //获取picturebox2的中心点 Rectangle pRec = this.pictureBox2.ClientRectangle; PointF pCenter = new PointF(pRec.Width / 2, pRec.Height / 2); ///图片置picturebox2的中心点的偏移量 float offsetX = pCenter.X - (img.Width / 2); float offsetY = pCenter.Y - (img.Height / 2); //图片旋转区域 其的中心点与picturebox2的中心点一致 RectangleF newpRect = new RectangleF(offsetX, offsetY, img.Width, img.Height); ///旋转点 PointF Pcenter = new PointF(pCenter.X, pCenter.Y);// new PointF(newpRect.X + newpRect.Width / 2, newpRect.Y + newpRect.Height / 2); //实现旋转 for (float i = 0; i < 36; i++) { g.Clear(pictureBox2.BackColor); //旋转角度 g.TranslateTransform(Pcenter.X, Pcenter.Y); g.RotateTransform(10F); g.TranslateTransform(-Pcenter.X, -Pcenter.Y); //显示图像到中心 g.DrawImage(img, newpRect);// new PointF(pRec.Width / 4, pRec.Height / 4)); Thread.Sleep(20); } } #endregion

 


posted @ 2015-01-20 15:12  wjshan0808  阅读(573)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3