在图像上设置水印,并将其保存在原有的品质 [英] Set watermark on an image and save it in an original quality
问题描述
我上传的网站上的照片,我想设置就可以了水印并将其保存在原有的品质。为了测试我创建C#应用程序。
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);
}
}
文件1的分辨率为为3648x2736
,大小 - 的4Mb
我不明白的第一件事就是为什么有水印 file1_withwatermark
是不是?
The first thing I don't understand is why does the file1_withwatermark
isn't with watermark?
和第二个是为什么的大小 file1_withwatermark
是的1Mb
,但它的分辨率为为3648x2736
呢!我想保存 file1_withwatermark
在原有的品质,那就是大小 file1_withwatermark
必须约为 4MB
。
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
.
推荐答案
两个感想:1)尝试更换保存和处理线;您可能必须保存之前未完成挂起的图形操作。 2)如果它是一个JPEG IMG,你需要指定的质量水平。见<一href=\"http://stackoverflow.com/questions/1669850/jpeg-com$p$pssion-with-high-quality-in-c-sharp\">Jpeg COM pression在C#高品质
Two thoughts: 1) Try swapping the save and dispose lines; you might have a pending graphics operation that is not completed before you save. 2) If it's a jpeg img, you need to specify the quality level. See Jpeg compression with high quality in c#
这篇关于在图像上设置水印,并将其保存在原有的品质的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!