图片缩放/旋转/平移/设置分辨率
#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