我正在调整图像大小并为图像添加水印。并返回图像。这是我的代码。
Image resizedImage = ResizeImage(original6, new Size(500, 375));
重新调整大小功能:
public static System.Drawing.Image ResizeImage(System.Drawing.Image image, Size size)
{
int newWidth;
int newHeight;
if (true)
{
int originalWidth = image.Width;
int originalHeight = image.Height;
float percentWidth = (float)size.Width / (float)originalWidth;
float percentHeight = (float)size.Height / (float)originalHeight;
float percent = percentHeight < percentWidth ? percentHeight : percentWidth;
newWidth = (int)(originalWidth * percent);
newHeight = (int)(originalHeight * percent);
}
else
{
newWidth = size.Width;
newHeight = size.Height;
}
System.Drawing.Image newImage = new Bitmap(newWidth, newHeight);
using (Graphics graphicsHandle = Graphics.FromImage(newImage))
{
graphicsHandle.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphicsHandle.DrawImage(image, 0, 0, newWidth, newHeight);
}
System.Drawing.Bitmap bitmapimage = new System.Drawing.Bitmap(newImage, size.Width, size.Height);// create bitmap with same size of Actual image
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmapimage);
SolidBrush brush = new SolidBrush(Color.FromArgb(113, 255, 255, 255));
//Adding watermark text on image
g.DrawString("My watermark", new Font("Arial", 16, FontStyle.Bold), brush, 5, 100);
return bitmapimage;
}
我正在检索带有水印的新尺寸图像并将另存为新图像文件。
resized6.Save(Server.MapPath(sSavePath + ownerRef + "Pic6v2" + ".jpg"));
这很好用。但是我无法控制文件大小。 当我的原始JPG只有45kb但我的新重新调整大小的图像是500kb时。如何减小文件大小。 info:原始分辨率(400x300像素)和新图像(500x375像素)
答案 0 :(得分:1)
我不记得这个,但文件大小通常与JPEG质量设置有关。你还需要确保它保存为一个实际的jpg,而不是一个位图,我没有看到你正在做...
另请参阅:C# Simple Image Resize : File Size Not Shrinking
JPEG质量设置: http://msdn.microsoft.com/en-us/library/bb882583.aspx
答案 1 :(得分:0)