I upload a photo on web site and I want to set a watermark on it and save it in original quality. For testing I create C# application.
class Class1
{
public static string GetContentType(String path)
{
switch (Path.GetExtension(path))
{
case ".bmp": return "Image/bmp";
case ".gif": return "Image/gif";
case ".jpg": return "Image/jpeg";
case ".png": return "Image/png";
default: break;
}
return String.Empty;
}
public static ImageFormat GetImageFormat(String path)
{
switch (Path.GetExtension(path).ToLower())
{
case ".bmp": return ImageFormat.Bmp;
case ".gif": return ImageFormat.Gif;
case ".jpg": return ImageFormat.Jpeg;
case ".png": return ImageFormat.Png;
default: return null;
}
}
public static void AddWaterMark(string sourceFile, string destinationPath)
{
// Normally you’d put this in a config file somewhere.
string watermark = "http://mysite.com/";
Image image = Image.FromFile(sourceFile);
Graphics graphic;
if (image.PixelFormat != PixelFormat.Indexed &&
image.PixelFormat != PixelFormat.Format8bppIndexed &&
image.PixelFormat != PixelFormat.Format4bppIndexed &&
image.PixelFormat != PixelFormat.Format1bppIndexed)
{
graphic = Graphics.FromImage(image);
}
else
{
Bitmap indexedImage = new Bitmap(image);
graphic = Graphics.FromImage(indexedImage);
// Draw the contents of the original bitmap onto the new bitmap.
graphic.DrawImage(image, 0, 0, image.Width, image.Height);
image = indexedImage;
}
graphic.SmoothingMode = SmoothingMode.AntiAlias & SmoothingMode.HighQuality;
Font myFont = new Font("Arial", 20);
SolidBrush brush = new SolidBrush(Color.FromArgb(80, Color.White));
//This gets the size of the graphic
SizeF textSize = graphic.MeasureString(watermark, myFont);
// Code for writing text on the image.
PointF pointF = new PointF(430, 710);
graphic.DrawString(watermark, myFont, brush, pointF);
image.Save(destinationPath, GetImageFormat(sourceFile));
graphic.Dispose();
}
}
//And using
class Program
{
private static string file1 = "C:\\1.JPG";
private static string file1_withwatermark = "C:\\1_withwatermark.JPG";
static void Main(string[] args)
{
Class1.AddWaterMark(file1, file1_withwatermark);
}
}
The resolution of file1 is 3648x2736
, size - 4Mb
.
The first thing I don't understand is why does the file1_withwatermark
isn't with watermark?
And the second one is why does the size of the file1_withwatermark
is 1Mb
, however the resolution of it is 3648x2736
too! I want to save file1_withwatermark
in original quality, that is the size of file1_withwatermark
must be about 4Mb
.