假设我有一张400x300px的图片,并且我希望将它剪成200x200px,将其置于服务器端(C#,.NET 4.0)中。剪下一张图片的中心
我该怎么办?使用某种画布并移动它?任何教程/代码示例/建议?
假设我有一张400x300px的图片,并且我希望将它剪成200x200px,将其置于服务器端(C#,.NET 4.0)中。剪下一张图片的中心
我该怎么办?使用某种画布并移动它?任何教程/代码示例/建议?
创建具有目标大小的新的位图对象。
围绕该位图创建图形对象。
在Graphcs对象上,使用合适的参数调用DrawImage(),并从第一张图片中切出适当的段。
代码将是这样的:
Bitmap dstBitmap=new Bitmap(200, 200);
using (Graphics g=Graphics.FromImage(dstBitmap))
{
srcBitmap.DrawImage(dstBitmap, /* cropping parameters here */);
}
// at the end you'll have your bitmap in dstBitmap, ...
我并没有包括对方法字面参数,使用智能感知和手册,以数字出来。
你是什么意思作为“在这里裁剪参数”? – markzzz 2012-04-20 08:53:15
尝试自己弄清楚参数。 DrawImage()方法有几个重载,你将不得不找到一个适合你的。您将必须定义一个Rect,其中包含要剪切的图像部分的坐标。 – 2012-04-20 11:39:19
尝试这样:
Bitmap sourceImage = ...;
int targetWidth = 200;
int targetHeight = 200;
int x = sourceImage.Width/2 - targetWidth/2;
int y = sourceImage.Height/2 - targetHeight/2;
Rectangle cropArea =
new Rectangle(x, y, targetWidth, targetHeight);
Bitmap targetImage =
sourceImage.Clone(cropArea, sourceImage.PixelFormat);
如果源图像比目标图像尺寸更小,这显然会失败,但你的想法。
此方法将保存图像的中心,如果需要裁剪:
bool SaveCroppedImage(Image image, int targetWidth, int targetHeight, string filePath)
{
ImageCodecInfo jpgInfo = ImageCodecInfo.GetImageEncoders().Where(codecInfo => codecInfo.MimeType == "image/jpeg").First();
Image finalImage = image;
System.Drawing.Bitmap bitmap = null;
try
{
int left = 0;
int top = 0;
int srcWidth = targetWidth;
int srcHeight = targetHeight;
bitmap = new System.Drawing.Bitmap(targetWidth, targetHeight);
double croppedHeightToWidth = (double)targetHeight/targetWidth;
double croppedWidthToHeight = (double)targetWidth/targetHeight;
if (image.Width > image.Height)
{
srcWidth = (int)(Math.Round(image.Height * croppedWidthToHeight));
if (srcWidth < image.Width)
{
srcHeight = image.Height;
left = (image.Width - srcWidth)/2;
}
else
{
srcHeight = (int)Math.Round(image.Height * ((double)image.Width/srcWidth));
srcWidth = image.Width;
top = (image.Height - srcHeight)/2;
}
}
else
{
srcHeight = (int)(Math.Round(image.Width * croppedHeightToWidth));
if (srcHeight < image.Height)
{
srcWidth = image.Width;
top = (image.Height - srcHeight)/2;
}
else
{
srcWidth = (int)Math.Round(image.Width * ((double)image.Height/srcHeight));
srcHeight = image.Height;
left = (image.Width - srcWidth)/2;
}
}
using (Graphics g = Graphics.FromImage(bitmap))
{
g.SmoothingMode = SmoothingMode.HighQuality;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.CompositingQuality = CompositingQuality.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(image, new Rectangle(0, 0, bitmap.Width, bitmap.Height), new Rectangle(left, top, srcWidth, srcHeight), GraphicsUnit.Pixel);
}
finalImage = bitmap;
}
catch { }
try
{
using (EncoderParameters encParams = new EncoderParameters(1))
{
encParams.Param[0] = new EncoderParameter(Encoder.Quality, (long)100);
//quality should be in the range [0..100] .. 100 for max, 0 for min (0 best compression)
finalImage.Save(filePath, jpgInfo, encParams);
return true;
}
}
catch { }
if (bitmap != null)
{
bitmap.Dispose();
}
return false;
}
我认为这是类似于这样的问题:http://stackoverflow.com/questions/18014365/c-sharp-crop-image -from-center/27164374#27164374 – h3n 2014-11-27 06:33:09