- [DEVEXPRESS] Hỗ trợ tìm kiếm highlight không dấu và không khoảng cách trên Gridview Filter
- [C#] Chia sẻ source code phần mềm Image Downloader tải hàng loạt hình ảnh từ danh sách link url
- [C#] Chụp hình và quay video từ camera trên winform
- [C#] Chia sẽ full source code tách file Pdf thành nhiều file với các tùy chọn
- Giới thiệu về Stock Tracker Widget - Công cụ theo dõi cổ phiếu và cảnh báo giá tăng giảm bằng C# và WPF
- [VB.NET] Chia sẻ công cụ nhập số tiền tự động định dạng tiền tệ Việt Nam
- [VB.NET] Hướng dẫn fill dữ liệu từ winform vào Microsoft word
- [VB.NET] Hướng dẫn chọn nhiều dòng trên Datagridview
- Hướng Dẫn Đăng Nhập Nhiều Tài Khoản Zalo Trên Máy Tính Cực Kỳ Đơn Giản
- [C#] Chia sẻ source code phần mềm đếm số trang tập tin file PDF
- [C#] Cách Sử Dụng DeviceId trong C# Để Tạo Khóa Cho Ứng Dụng
- [SQLSERVER] Loại bỏ Restricted User trên database MSSQL
- [C#] Hướng dẫn tạo mã QRcode Style trên winform
- [C#] Hướng dẫn sử dụng temp mail service api trên winform
- [C#] Hướng dẫn tạo mã thanh toán VietQR Pay không sử dụng API trên winform
- [C#] Hướng Dẫn Tạo Windows Service Đơn Giản Bằng Topshelf
- [C#] Chia sẻ source code đọc dữ liệu từ Google Sheet trên winform
- [C#] Chia sẻ source code tạo mã QR MOMO đa năng Winform
- [C#] Chia sẻ source code phần mềm lên lịch tự động chạy ứng dụng Scheduler Task Winform
- [C#] Hướng dẫn download file từ Minio Server Winform
[C#] Hướng dẫn viết ứng dụng xoay hình ảnh (Rotate Image)
Xin chào các bạn, bài viết hôm nay mình sẽ hướng dẫn các bạn cách xoay hình ảnh trong lập trình Csharp và save chúng lại thành một file hình ảnh.
[C#] ROTATE IMAGE WINFORM
Chức năng này hỗ trợ, cho các bạn nào có viết ứng dụng xem hình bằng winform Csharp.
Dưới đây là demo ứng dụng xoay hình C#:

Ở source code bên dưới, mình đã có hàm sẵn RotateBitmap, các bạn chỉ cần truyền tham số hình ảnh vào góc (angle) vào để chạy thôi nhé.
Full source code C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Rotate_Image
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private Bitmap OriginalBitmap;
        private Bitmap RotatedBitmap;
        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            txtAngle.Text = trackBar1.Value.ToString();
            float angle = float.Parse(txtAngle.Text);
            RotatedBitmap = RotateBitmap(OriginalBitmap, angle);
            picRotated.Image = RotatedBitmap;
            SizeForm();
        }
        private void btnLoad_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofdFile = new OpenFileDialog();
            if (ofdFile.ShowDialog() == DialogResult.OK)
            {
                OriginalBitmap = new Bitmap(ofdFile.FileName);
                picRotated.Image = OriginalBitmap;
                picRotated.Visible = true;
            }
        }
        private Bitmap RotateBitmap(Bitmap bm, float angle)
        {
            Matrix rotate_at_origin = new Matrix();
            rotate_at_origin.Rotate(angle);
            PointF[] points =
            {
                new PointF(0, 0),
                new PointF(bm.Width, 0),
                new PointF(bm.Width, bm.Height),
                new PointF(0, bm.Height),
            };
            rotate_at_origin.TransformPoints(points);
            float xmin, xmax, ymin, ymax;
            GetPointBounds(points, out xmin, out xmax, out ymin, out ymax);
            int wid = (int)Math.Round(xmax - xmin);
            int hgt = (int)Math.Round(ymax - ymin);
            Bitmap result = new Bitmap(wid, hgt);
            Matrix rotate_at_center = new Matrix();
            rotate_at_center.RotateAt(angle,
                new PointF(wid / 2f, hgt / 2f));
            using (Graphics gr = Graphics.FromImage(result))
            {
                gr.InterpolationMode = InterpolationMode.High;
                gr.Clear(bm.GetPixel(0, 0));
                gr.Transform = rotate_at_center;
                int x = (wid - bm.Width) / 2;
                int y = (hgt - bm.Height) / 2;
                gr.DrawImage(bm, x, y);
            }
            return result;
        }
        private void GetPointBounds(PointF[] points, out float xmin, out float xmax, out float ymin, out float ymax)
        {
            xmin = points[0].X;
            xmax = xmin;
            ymin = points[0].Y;
            ymax = ymin;
            foreach (PointF point in points)
            {
                if (xmin > point.X) xmin = point.X;
                if (xmax < point.X) xmax = point.X;
                if (ymin > point.Y) ymin = point.Y;
                if (ymax < point.Y) ymax = point.Y;
            }
        }
        private void SizeForm()
        {
            int wid = picRotated.Right + picRotated.Left;
            int hgt = picRotated.Bottom + picRotated.Left;
            this.ClientSize = new Size(
                Math.Max(wid, this.ClientSize.Width),
                Math.Max(hgt, this.ClientSize.Height));
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfdFile = new SaveFileDialog();
            if (sfdFile.ShowDialog() == DialogResult.OK)
            {
                SaveBitmapUsingExtension(RotatedBitmap, sfdFile.FileName);
            }
        }
        public void SaveBitmapUsingExtension(Bitmap bm,
            string filename)
        {
            string extension = Path.GetExtension(filename);
            switch (extension.ToLower())
            {
                case ".bmp":
                    bm.Save(filename, ImageFormat.Bmp);
                    break;
                case ".exif":
                    bm.Save(filename, ImageFormat.Exif);
                    break;
                case ".gif":
                    bm.Save(filename, ImageFormat.Gif);
                    break;
                case ".jpg":
                case ".jpeg":
                    bm.Save(filename, ImageFormat.Jpeg);
                    break;
                case ".png":
                    bm.Save(filename, ImageFormat.Png);
                    break;
                case ".tif":
                case ".tiff":
                    bm.Save(filename, ImageFormat.Tiff);
                    break;
                default:
                    throw new NotSupportedException(
                        "Unknown file extension " + extension);
            }
        }
    }
}HAPPY CODING 

![[C#] Hướng dẫn viết ứng dụng xoay hình ảnh (Rotate Image)](https://laptrinhvb.net/uploads/users/9a8cb514e4428e85fb4ca07588e9103f.png)

![[C#] Hướng dẫn sử dụng công cụ Checked ListBox trên Winform](https://laptrinhvb.net/uploads/source/devexpress/CheckedListBoxDemo_thumb.jpg)
![[C#] Kiểm tra phiên bản Microsoft Office đang sử dụng trên máy tính](https://laptrinhvb.net/uploads/source/new_image_baiviet/check_microsoft_office_verison.png)
![[C#] Tìm kiếm file trùng nhau trong cùng thư mục lập trình Winform](https://laptrinhvb.net/uploads/source/csharp/duplicate_file_thumb.png)
![[C#] Hướng dẫn Scan file hình ảnh trên máy scanner sử dụng WIA API](https://laptrinhvb.net/uploads/source/image_baiviet/c09991bd71d43786d784443b00e449b2.jpg)
![[C#] Hướng dẫn tạo Gradient Background trên Winform](https://laptrinhvb.net/uploads/source/new_image_baiviet/gadient_winform_background.png)
![[C#] Hướng dẫn thiết kế BaseForm sử dụng Template Method Pattern](https://laptrinhvb.net/uploads/source/new_image_baiviet/template_method_design_pattern.png)
![[C#] Chặn chức năng chụp màn hình ứng dụng (print screen) trong winform](https://laptrinhvb.net/uploads/source/image_baiviet/b0fa0b8a886eb9a386cd88a9e9c5fcb8.jpg)
![[C#] Theo dõi sử kiện process Start hay Stop trên Winform](https://laptrinhvb.net/uploads/source/new_image_baiviet/watch_event_process.png)
![[C#] Hướng dẫn Rotate and Flip Screen Monitor trên PC](https://laptrinhvb.net/uploads/source/new_image_baiviet/rotate-screen-csharp.jpg)
![[C#] Chèn chú thích dưới hình ảnh lập trình Winform](https://laptrinhvb.net/uploads/source/csharp/footer_image_csharp.png)
![[C#] Hướng dẫn xem lịch sử các trang web đã truy cập trên Chrome Browser](https://laptrinhvb.net/uploads/source/vbnet/HistoryChrome_thumb.jpg)
![[C#] Hướng dẫn Thêm, xóa, sửa với PostgreSQL winform](https://laptrinhvb.net/uploads/source/new_image_baiviet/screenshot_1686208193.png)
![[C#] Hướng dẫn viết ứng dụng đo nhiệt độ CPU đang hoạt động](https://laptrinhvb.net/uploads/source/csharp/temp_cpu_thumb.png)
![[C#] Giới thiệu thư viện Playwright của Microsoft automate browsers like Selemium](https://laptrinhvb.net/uploads/source/new_image_baiviet/playwright_thumb.png)
![[C#] Hướng dẫn download file từ Minio Server Winform](https://laptrinhvb.net/uploads/source/new_image_baiviet/Minio_demo.png)
![[C#] Hướng dẫn mở nhiều ứng dụng zalo trên Desktop](https://laptrinhvb.net/uploads/source/new_image_baiviet/zalo_instance_csharp.png)
![[C#] Điều khiển ứng dụng từ xa và rất xa với Telegram Bot Winform](https://laptrinhvb.net/uploads/source/vbnet/remote_telegram_csharp.jpg)
![[C#] Hướng dẫn sử dụng Row Filter trên DataView của DataTable](https://laptrinhvb.net/uploads/source/csharp/dataview_filter_csharp_thumb.jpg)
![[C#] Hướng dẫn cách tạo mã QR Code trên file Excel](https://laptrinhvb.net/uploads/source/new_image_baiviet/qrcode_to_excel.png)
![[C#] Chụp hình và quay video từ camera trên winform](https://laptrinhvb.net/uploads/source/new_image_baiviet/thumb_recordvideo_camera_csharp.png)
![[C#] Hướng dẫn viết ứng dụng check live SSH Server](https://laptrinhvb.net/uploads/source/image_baiviet/6bc4d5b0ca4995dfce771849892f925e.jpg)
![[C#] Hướng  dẫn ẩn và khóa button x đóng form trên Winform](https://laptrinhvb.net/uploads/source/csharp/close_button_csharp_thumb.jpg)
![[C# - ANDROID] Viết ứng dụng android và Csharp thành máy quét mã vạch Qrcode](https://laptrinhvb.net/uploads/source/image_baiviet/6719a7afa36fd2b5180b6b20b12708ec.jpg)
![[C#] Tách file thành nhiều phần để download](https://laptrinhvb.net/uploads/source/image_baiviet/de1a2476a93e212f093115ec3940926d.png)
![[C#] Xem dung lượng RAM đang sử dụng trong ứng dụng Winform](https://laptrinhvb.net/uploads/source/csharp/ram_using_csharp_thumb.png)
