how can I add watermark image at left bottom of a directory's pictures by using FileStream?

  • Thread starter Thread starter Amin-
  • Start date Start date
A

Amin-

Guest
I found C# code for add a watermark text to photos of a directory by FileStream. but I have problem with that....


- How can I coordinate the picture from bottom left(for adding an "watermark Image"(not text watermark) to the "bottom left" corner of all pictures)?

Note: This code will add watermark text to the upper left side of all pictures in a directory.

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Imaging;

namespace WatermarkImages
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.folderBrowserDialog1.Description =
"Select the images directory";

// Disallow creation of new files using the FolderBrowserDialog.
this.folderBrowserDialog1.ShowNewFolderButton = false;
}

//click button btnWatermark
private void btnWatermark_Click(object sender, EventArgs e)
{
string path = String.Empty;
DialogResult result = folderBrowserDialog1.ShowDialog();
if (result == DialogResult.OK)
{
path = folderBrowserDialog1.SelectedPath;
}

if (path == String.Empty)
{
lblStatus.Text = "Invalid directory..";
return;
}


lblStatus.Text = String.Empty;
Image img = null;
string fullPath = String.Empty;

try
{
string[] imgExtension = { "*.jpg", "*.jpeg", ".gif", "*.bmp" };
List<FileInfo> files = new List<FileInfo>();

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, "Watermark text", 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();

}
}
lblStatus.Text = "Processing Completed";
}
catch (Exception ex)
{
lblStatus.Text = "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("Verdana", 16, FontStyle.Bold, GraphicsUnit.Pixel);
//Adds a transparent watermark with an 100 alpha value.
Color color = Color.FromArgb(100, 0, 0, 0);
//The position where to draw the watermark on the image
Point pt = new Point(10, 30);
SolidBrush sbrush = new SolidBrush(color);

Graphics gr = null;
try
{
gr = Graphics.FromImage(img);
}
catch
{
// http://support.microsoft.com/Default.aspx?id=814675
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);
}

}
}

Continue reading...
 
Back
Top