- [VB.NET] Lấy địa chỉ Bios Serial Number trên Winform
- [C#] Giới thiệu và sử dụng thư viện AutoMapper
- [DEVEXPRESS] Hướng dẫn Custom Summary in Footer trong Gridview C#
- [C#] Dependency Injection in Winform
- [SQLSERVER] Hướng dẫn tìm kiếm nâng cao trên sql
- [C#] Hướng dẫn sử dụng SetTimeOut trên Winform like Javascript
- [DATABASE] In cây thông noel bằng sqlserver
- [C#] Hướng dẫn fix lỗi hiển thị UTF-8 khi sử dụng WebClient Download String
- [DATABASE] Hướng dẫn mã hóa và giải mã sử dụng thuật toán AES 256 trên sqlserver
- [DATABASE] Base64 Encode and Decode trong Sqlserver
- [C#] Vì Mẹ anh bắt phải Fake địa chỉ MacAddress
- [C#] Hướng dẫn xuất dữ liệu từ DataGridview ra file Excel
- [C#] Hướng dẫn khởi động lại chương trình ứng dụng winform
- [C#] Sự khác nhau giữa String.IsNullOrEmpty và String.IsNullOrWhiteSpace
- [C#] Hướng dẫn đọc file hình ảnh định dạng WEBP và chuyển đổi WebP sang JPG
- [C#] Kiểm tra phiên bản Microsoft Office đang sử dụng trên máy tính
- [C#] Hướng dẫn chuyển đổi tập tin hình ảnh XPS sang Bitmap
- [C#] Giới thiệu Component WebView2 của Microsoft
- [C#] Hướng dẫn lưu tất cả hình ảnh từ File Excel vào thư mục window
- [DATABASE] Hướng dẫn import và export hình ảnh image từ Sqlserver
[C#] Hướng dẫn thêm text water mark vào hàng loạt image trong một folder
Bài viết hôm nay, mình sẽ hướng dẫn các bạn cách chèn Text Watermark C# vào hàng loạt hình ảnh đã có sẵn trong thư mục .
Thường các bạn có làm website thì các hình ảnh của bạn không muốn người khác copy thì các bạn thường thêm watermark vào hình ảnh kèm theo, để người khác không thể sử dụng được hình ảnh của các bạn.
Thông thường thì các bạn lập trình web, sẽ hỗ trợ sẵn là khi up hình lên sẽ tự động chèn thêm watermark.
Còn trong bài viết này, mình đã có một folder chứa hàng nghìn tấm ảnh, và mình muốn thêm water mark vào cùng một lúc thì sử dụng thế nào.
Hình ảnh dưới đây, mình đã thêm watermark "https://laptrinhvb.net" và tấm hình của Lưu Diệc Phi.
Dưới đây là source code chèn watermark C#, các bạn có thể đọc và chỉnh sửa các thông tin cho phù hợp nhé.
Source code Add Watermark to Image C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AddWaterMaskImages
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.folderBrowserDialog1.Description =
"Chọn hình ảnh từ thư mục";
this.folderBrowserDialog1.ShowNewFolderButton = false;
}
private void button1_Click(object sender, EventArgs e)
{
string path = String.Empty;
DialogResult result = folderBrowserDialog1.ShowDialog();
if (result == DialogResult.OK)
{
path = folderBrowserDialog1.SelectedPath;
}
if (path == String.Empty)
{
return;
}
Image img = null;
string fullPath = String.Empty;
try
{
string[] imgExtension = { "*.jpg", "*.jpeg", ".gif", "*.bmp" };
List files = new List();
DirectoryInfo dir = new DirectoryInfo(path);
foreach (string ext in imgExtension)
{
FileInfo[] folder = dir.GetFiles(ext, SearchOption.AllDirectories);
foreach (FileInfo file in folder)
{
FileStream fs = file.OpenRead();
fullPath = path + @"" + file.Name;
Stream outputStream = new MemoryStream();
AddWatermark(fs, "https://laptrinhvb.net", outputStream);
fs.Close();
file.Delete();
img = Image.FromStream(outputStream);
using (Bitmap savingImage = new Bitmap(img.Width, img.Height, img.PixelFormat))
{
using (Graphics g = Graphics.FromImage(savingImage))
g.DrawImage(img, new Point(0, 0));
savingImage.Save(fullPath, ImageFormat.Jpeg);
}
img.Dispose();
}
}
MessageBox.Show("Processing Completed");
}
catch (Exception ex)
{
MessageBox.Show("There was an error during processing..");
}
finally
{
if (img != null)
img.Dispose();
}
}
public void AddWatermark(FileStream fs, string watermarkText, Stream outputStream)
{
Image img = Image.FromStream(fs);
Font font = new Font("Tahoma", 48, FontStyle.Bold, GraphicsUnit.Pixel);
//Color color = Color.FromArgb(100, 0, 0, 0);
Color color = Color.Orange;
Point pt = new Point(10, 30);
SolidBrush sbrush = new SolidBrush(color);
Graphics gr = null;
try
{
gr = Graphics.FromImage(img);
}
catch
{
Image img1 = img;
img = new Bitmap(img.Width, img.Height);
gr = Graphics.FromImage(img);
gr.DrawImage(img1, new Rectangle(0, 0, img.Width, img.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel);
img1.Dispose();
}
gr.DrawString(watermarkText, font, sbrush, pt);
gr.Dispose();
img.Save(outputStream, ImageFormat.Jpeg);
}
}
}
HAPPY CODING