winform 图像(Bitmap)变换(平移、旋转、缩放)的一些方法

先看效果

平移

旋转

 缩放

 

对应方法 

平移

        /// <summary>
        /// 
        /// </summary>
        /// <param name="bmp">原始图像</param>
        /// <param name="x">横向平移距离(像素)</param>
        /// <param name="y">纵向平移距离(像素)</param>
        /// <param name="bkColor"></param>
        /// <returns></returns>
        public static Bitmap Move(Bitmap bmp, int x, int y, Color bkColor)
        {
            Bitmap backBitmap = new Bitmap(bmp.Width + x, bmp.Height + y);
            Graphics g = Graphics.FromImage(backBitmap);
            g.Clear(bkColor);
            g.DrawImageUnscaled(bmp, x, y);
            g.Dispose();
            return backBitmap;
        }

 旋转

        /// <summary>
        /// 旋转图像
        /// </summary>
        /// <param name="bmp">原始图像</param>
        /// <param name="angle">旋转角度(角度制)</param>
        /// <param name="backColor">背景色</param>
        /// <returns>输出Bitmap</returns>
        public static Bitmap Rotate(Bitmap bmp, float angle, Color backColor)
        {
            int w = bmp.Width + 2;
            int h = bmp.Height + 2;

            PixelFormat pf;

            if (backColor == Color.Transparent)
            {
                pf = PixelFormat.Format32bppArgb;
            }
            else
            {
                pf = bmp.PixelFormat;
            }

            Bitmap tmp = new Bitmap(w, h, pf);
            Graphics g = Graphics.FromImage(tmp);
            g.Clear(backColor);
            g.DrawImageUnscaled(bmp, 1, 1);
            g.Dispose();

            GraphicsPath path = new GraphicsPath();
            path.AddRectangle(new RectangleF(0f, 0f, w, h));
            Matrix mtrx = new Matrix();
            mtrx.Rotate(angle);
            RectangleF rct = path.GetBounds(mtrx);

            Bitmap dst = new Bitmap((int)rct.Width, (int)rct.Height, pf);
            g = Graphics.FromImage(dst);
            g.Clear(backColor);
            g.TranslateTransform(-rct.X, -rct.Y);
            g.RotateTransform(angle);
            g.InterpolationMode = InterpolationMode.HighQualityBilinear;
            g.DrawImageUnscaled(tmp, 0, 0);
            g.Dispose();

            tmp.Dispose();

            return dst;
        }

缩放

        /// <summary>
        /// 缩放图像
        /// </summary>
        /// <param name="bitmap">原始图像</param>
        /// <param name="scaleX">横向缩放(比例)</param>
        /// <param name="scaleY">纵向缩放(比例)</param>
        /// <returns></returns>
        public static Bitmap Scale(Bitmap bitmap, float scaleX, float scaleY)
        {
            if (scaleX < 0) bitmap.RotateFlip(RotateFlipType.RotateNoneFlipX);
            if (scaleY < 0) bitmap.RotateFlip(RotateFlipType.RotateNoneFlipY);
            return new Bitmap(bitmap, new Size((int)Math.Abs(bitmap.Width * scaleX), (int)Math.Abs(bitmap.Height * scaleY)));
        }

参考:https://www.cnblogs.com/hetonghai/p/1211394

Windows Forms中,如果你想让一个位图BitmapBitmap)按照Graphics对象的大小自动缩放,你可以通过下面的方式来实现: 1. **创建Graphics对象**: 首先,获取一个Graphics对象,通常是基于某个PictureBox、Panel或其他绘图容器。 ```csharp Graphics graphics = pictureBox1.CreateGraphics(); // 假设pictureBox1是你想操作的控件 ``` 2. **计算目标尺寸**: 获取Graphics对象的Size属性,这将是你要缩放Bitmap的新尺寸。 ```csharp Size newSize = graphics.Size; ``` 3. **创建放大后的Bitmap**: 如果需要保持比例,可以使用`Image.Clone`或`Bitmap.GetHbitmap`创建新的Bitmap。如果需要等比缩放,可以调整宽高分别除以原有宽度和高度。 ```csharp int originalWidth = bitmap.Width; int originalHeight = bitmap.Height; double ratioX = (double)newSize.Width / originalWidth; double ratioY = (double)newSize.Height / originalHeight; int newWidth = (int)Math.Round(originalWidth * ratioX); int newHeight = (int)Math.Round(originalHeight * ratioY); Bitmap scaledBitmap = new Bitmap(newWidth, newHeight); using (Graphics gScaled = Graphics.FromImage(scaledBitmap)) { gScaled.DrawImage(bitmap, 0, 0, newWidth, newHeight); } ``` 4. **显示或保存放大后的Bitmap**: 将放大后的Bitmap设置回对应的控件,或者将其保存到磁盘。 ```csharp pictureBox1.Image = scaledBitmap; // 显示在PictureBox中 //scaledBitmap.Save("scaled_bitmap.jpg", ImageFormat.Jpeg); // 保存到文件 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值