Suppose I有400x300px图像,我想将其切割为200x200px,在服务器一侧(C#, NET 4.0)。
我如何能够这样做? 使用某种血管和移动吗? 任何辅导/编码实例/建议?
Suppose I有400x300px图像,我想将其切割为200x200px,在服务器一侧(C#, NET 4.0)。
我如何能够这样做? 使用某种血管和移动吗? 任何辅导/编码实例/建议?
类似情况:
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;
}
Create NEW Bitmap object, with the destination size.
围绕该比图制作图表。
在该图表中,有适当参数的“绘画”,将从第一个图像中删除适当的部分。
法典中将包括:
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, ...
我没有在字面上列出这些方法的参数,使用电话和人工表示。
What is the use of default keyword in C#? Is it introduced in C# 3.0 ?
I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...
I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...
I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...
I have two EF entities. One has a property called HouseNumber. The other has two properties, one called StartHouseNumber and one called EndHouseNumber. I want to create a many to many association ...
How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...
Since I cannot order my dictionary, what is the best way of going about taking key value pairs and also maintaing an index?
Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...