.net 图片操作

发布于:2024-05-03 ⋅ 阅读:(30) ⋅ 点赞:(0)

图片操作 bitmap 旋转 bitmap左右镜像




        /// <summary>
        /// bitmap角度旋转
        /// </summary>
        /// <param name="image"></param>
        /// <param name="angle"></param>
        /// <returns></returns>
        public static Bitmap RotateImage(Bitmap image, int angle)
        {
            try
            {
                // 创建一个新的Bitmap对象,宽度和高度互换
                Bitmap rotatedImage = new Bitmap(image.Height, image.Width);

                // 创建一个Graphics对象,并设置其绘制原点为新图像的中心点
                using (Graphics g = Graphics.FromImage(rotatedImage))
                {
                    g.TranslateTransform(rotatedImage.Width / 2, rotatedImage.Height / 2);

                    // 设置旋转角度为90度
                    g.RotateTransform(angle);

                    // 将原图像绘制到新图像上
                    g.DrawImage(image, -image.Width / 2, -image.Height / 2, image.Width, image.Height);
                    return rotatedImage;
                }
            }
            catch (AccessViolationException ave)
            {
                return null;
            }

        }
        /// <summary>
        ///  bitmap角度旋转
        /// </summary>
        /// <param name="originalImage"></param>
        /// <param name="angle"></param>
        /// <returns></returns>
        public static Bitmap RotateImage2(Bitmap originalImage, float angle)
        {
            // 计算旋转后的包围矩形大小
            int width = originalImage.Width;
            int height = originalImage.Height;
            double radians = angle * Math.PI / 180.0;
            double sin = Math.Abs(Math.Sin(radians));
            double cos = Math.Abs(Math.Cos(radians));
            int newWidth = (int)Math.Floor(width * cos + height * sin);
            int newHeight = (int)Math.Floor(width * sin + height * cos);

            // 创建一个新的Bitmap,大小足够容纳旋转后的图像
            Bitmap rotatedImage = new Bitmap(newWidth, newHeight);

            using (Graphics g = Graphics.FromImage(rotatedImage))
            {
                // 设置插值模式和平滑模式
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                g.SmoothingMode = SmoothingMode.HighQuality;
                g.PixelOffsetMode = PixelOffsetMode.HighQuality;
                g.CompositingQuality = CompositingQuality.HighQuality;

                // 计算旋转后的位置,以便将原始图像绘制到新的Bitmap中心
                Point center = new Point(newWidth / 2, newHeight / 2);
                g.TranslateTransform(center.X, center.Y);
                g.RotateTransform(angle);
                g.TranslateTransform(-center.X, -center.Y);

                // 在新的Bitmap上绘制旋转后的图像
                g.DrawImage(originalImage, new Point((newWidth - width) / 2, (newHeight - height) / 2));
            }


            return rotatedImage;
        }

        /// <summary>
        /// 图像镜像转换
        /// </summary>
        /// <param name="originalBitmap"></param>
        /// <returns></returns>
        public static Bitmap BitmapImage2(Bitmap originalImage)
        {
            {
                // 创建一个与原始图片大小相同的空白图片
                Bitmap flippedImage = new Bitmap(originalImage.Width, originalImage.Height);

                // 获取空白图片的绘图对象
                using (Graphics g = Graphics.FromImage(flippedImage))
                {
                    // 水平翻转图片
                    g.DrawImage(originalImage, new Rectangle(0, 0, flippedImage.Width, flippedImage.Height),
                        new Rectangle(originalImage.Width, 0, -originalImage.Width, originalImage.Height),
                        GraphicsUnit.Pixel);
                }

                // 保存翻转后的图片
                return flippedImage;
            }
        }

        public static Bitmap BitmapImage(Bitmap originalBitmap)
        {
            // 创建一个新的Bitmap,大小与原始相同
            Bitmap mirroredBitmap = new Bitmap(originalBitmap.Width, originalBitmap.Height);

            // 循环遍历原始Bitmap的像素
            for (int y = 0; y < originalBitmap.Height; y++)
            {
                for (int x = 0; x < originalBitmap.Width; x++)
                {
                    // 获取原始Bitmap中的像素颜色
                    Color originalColor = originalBitmap.GetPixel(x, y);

                    // 计算水平翻转后的x坐标
                    int mirroredX = originalBitmap.Width - 1 - x;

                    // 在新Bitmap上设置镜像像素
                    mirroredBitmap.SetPixel(mirroredX, y, originalColor);
                }
            }

            return mirroredBitmap;
        }