- [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
Viết ứng dụng giảm dung lượng hình ảnh (Compression Image) sử dụng VB.NET
Hôm nay, mình viết chương trình giảm dung lượng, chất lượng của hình ảnh, ví dụ: bạn có file hình 3MB bạn muốn nén nó xuống vài trăm KB để up lên website hay chạy ứng dụng để giảm tải băng thông...
Chương trình có giao diện như mình demo bên dưới:

Chương trình giảm dung lượng của mình sử dụng hàm EncoderParameter trong thư viện System.Drawing.Imaging của VB.NET
+ Đầu bạn import thư viện system. drawing . imaging vào:
Imports System.Drawing.Imaging+ Tiếp theo , mình viết một function GetEncoderInfo() để get mime của hình ảnh:
Private Function GetEncoderInfo(ByVal mimeType As String) As ImageCodecInfo
        Dim j As Integer
        Dim encoders As ImageCodecInfo()
        encoders = ImageCodecInfo.GetImageEncoders()
        For j = 0 To encoders.Length
            If encoders(j).MimeType = mimeType Then
                Return encoders(j)
            End If
        Next j
        Return Nothing
    End Function+ Tiếp theo, mình viêt hàm SaveJPGWithCompressionSetting() thành file hình khác, sau khi đã tinh chỉnh các thông số cho hình ảnh:
Private Sub SaveJPGWithCompressionSetting(ByVal image As Image, ByVal szFileName As String, ByVal lCompression As Long)
        On Error GoTo chkErr
        Dim eps As EncoderParameters = New EncoderParameters(1)
        eps.Param(0) = New EncoderParameter(Encoder.Quality, lCompression)
        Dim ici As ImageCodecInfo = GetEncoderInfo("image/jpeg")
        image.Save(szFileName, ici, eps)
        Exit Sub
chkErr: MsgBox("Error: " & Err.Number & " " & Err.Description & vbCrLf & "Choose a different name for file.")
        errOcr = True
        Resume Next
    End Sub+ Viết sự kiện, khi trackbar scroll để lấy giá trị chất lượng của hình ảnh mình muốn thay đổi, và lấy dung lượng size của hình ảnh:
Private Sub TrackBar1_Scroll(sender As Object, e As EventArgs) Handles TrackBar1.Scroll
        ToolTip1.SetToolTip(TrackBar1, TrackBar1.Value.ToString())
        'Release loaded file temp.jpg
        If Not (pbPic.Image Is Nothing) Then
            pbPic.Image.Dispose()
            pbPic.Image = Nothing
        End If
        '=====
        'Release temp100.jpg after load
        Dim bmi As Bitmap = Image.FromFile(Application.StartupPath & "	emp100.jpg")
        pbPic.Image = bmi
        SaveJPGWithCompressionSetting(pbPic.Image, Application.StartupPath & "	emp.jpg", Val(TrackBar1.Value.ToString()))
        bmi.Dispose()
        '====
        pbPic.Image = Image.FromFile(Application.StartupPath & "	emp.jpg")
        lblCI.Text = TrackBar1.Value.ToString() + "%"
        Dim FileSize As Long
        Dim suffit As String
        FileSize = FileLen(Application.StartupPath & "	emp.jpg")
        If FileSize < 1000 Then
            suffit = " Bytes"
            GoTo showit
        End If
        If FileSize > 1000000 Then
            FileSize = Int(FileSize / 1000000)
            suffit = " Mb"
            GoTo showit
        Else
            FileSize = Int(FileSize / 1000)
            suffit = " Kb"
        End If
showit: lblTemp.Text = FileSize & suffit
    End Sub+ Viết sự kiện cho nút lưu hình:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles btnLuuhinh.Click
        errOcr = False
        fdSaveAs.Title = "Select the image to Save As."
        fdSaveAs.Filter = "Jpeg Images(.jpg)|*.jpg"
        If fdSaveAs.ShowDialog() = DialogResult.OK Then
            'Allow pbPic.Image to be saved to temp.jpg
            Dim bm As Bitmap = Image.FromFile(Application.StartupPath & "	emp100.jpg")
            'Release loaded file temp.jpg
            If Not (pbPic.Image Is Nothing) Then
                pbPic.Image.Dispose()
                pbPic.Image = Nothing
            End If
            '=====
            SaveJPGWithCompressionSetting(bm, fdSaveAs.FileName, Val(TrackBar1.Value.ToString()))
            'Still error if you try to save pbPic.Image to temp100.jpg
            If errOcr Then
                errOcr = False
                pbPic.Image = bm
                Exit Sub
            End If
            bm.Dispose()
            pbPic.Image = Image.FromFile(fdSaveAs.FileName)
        End If
    End Sub+ Viết sự kiện mở file hình lên:
Private Sub btnChonhinh_Click(sender As Object, e As EventArgs) Handles btnChonhinh.Click
        OpenFileDialog1.Filter = "Jpeg Images(.jpg)|*.jpg"
        If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            pbPic.Image = New Bitmap(OpenFileDialog1.FileName)
            Dim FileSize As Long
            Dim suffit As String
            Dim i As Integer
            SaveJPGWithCompressionSetting(pbPic.Image, Application.StartupPath & "	emp100.jpg", 100)
            SaveJPGWithCompressionSetting(pbPic.Image, Application.StartupPath & "	emp.jpg", 100)
            FileSize = FileLen(Application.StartupPath & "	emp.jpg")
            If FileSize < 1000 Then
                suffit = " Bytes"
                GoTo showit
            End If
            If FileSize > 1000000 Then
                FileSize = Int(FileSize / 1000000)
                suffit = " Mb"
                GoTo showit
            Else
                FileSize = Int(FileSize / 1000)
                suffit = " Kb"
            End If
showit:     lbl100.Text = FileSize & suffit
        End If
    End Sub+ Và cuối cùng là mình viết sự kiện form_closing để ngừng xử lý của chương trình đến file ảnh:
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
        On Error GoTo chkErr
        'The Kill Function will not work if file is being used in a process
        pbPic.Image.Dispose() 'temp.jpg was in pbPic.Image
        Kill(Application.StartupPath & "	emp.jpg")
        Kill(Application.StartupPath & "	emp100.jpg")
        Exit Sub
chkErr: MsgBox("Error: " & Err.Number & " " & Err.Description & vbCrLf & "File will not be deleted :(")
        Resume Next
    End SubChúc các bạn thành công.






![[VB.NET] Hướng dẫn giải captcha sử dụng dịch vụ AZCaptcha API trên winform](https://laptrinhvb.net/uploads/source/vbnet/az_captchathumb.png)
![[VB.NET] Chặn website sử dụng file host windows](https://laptrinhvb.net/uploads/source/image_baiviet/b9e53bb3136859eecb6aae3836b57d09.jpg)


![[VB.NET] Autocomplete Textbox load dữ liệu từ database](https://laptrinhvb.net/uploads/source/image_baiviet/4a1971e8680f509b407851771044eb5d.png)


![[VB.NET] Viết ứng dụng chia sẽ mạng wifi internet trong LAN hotpost](https://laptrinhvb.net/uploads/source/image_baiviet/848264726cdda6fb6fd3e5b92a034363.jpg)



![[VB.NET] Viết ứng dụng chạy có tham số Arguments Params](https://laptrinhvb.net/uploads/source/image_baiviet/cbc5f7258b1d1cb18c6e84e092a2ca66.png)
![[VB.NET] Sercurity Winform with password - Yêu cầu nhập mật khẩu khi đóng form](https://laptrinhvb.net/uploads/source/image_baiviet/f97244b181c5aa8106a3e5328800d367.gif)

![[VB.NET] Chia sẽ source code gởi và nhận tin nhắn SMS từ Dcom 3G sử dụng AT Command](https://laptrinhvb.net/uploads/source/image_baiviet/0658e75e0221021a1dcae4f7e34a93c5.jpg)



![[VB.NET] Lập trình tự động đăng bài viết lên diễn đàn Xenforo](https://laptrinhvb.net/uploads/source/image_baiviet/a4c64d07d0bbbc1ad026edc56486665e.png)

![[VB.NET] Trình chiếu Power Point trên Winform](https://laptrinhvb.net/uploads/source/vbnet/THUMB_POWER.png)

