如何使用c#裁剪图像?
当前回答
我正在寻找一个简单和快速的函数,没有额外的库来做这项工作。我尝试了尼克斯解决方案,但我需要29.4秒来“提取”一个atlas文件的1195张图像。所以后来我用这种方法做了同样的工作,需要2.43秒。也许这个会有帮助。
// content of the Texture class
public class Texture
{
//name of the texture
public string name { get; set; }
//x position of the texture in the atlas image
public int x { get; set; }
//y position of the texture in the atlas image
public int y { get; set; }
//width of the texture in the atlas image
public int width { get; set; }
//height of the texture in the atlas image
public int height { get; set; }
}
Bitmap atlasImage = new Bitmap(@"C:\somepicture.png");
PixelFormat pixelFormat = atlasImage.PixelFormat;
foreach (Texture t in textureList)
{
try
{
CroppedImage = new Bitmap(t.width, t.height, pixelFormat);
// copy pixels over to avoid antialiasing or any other side effects of drawing
// the subimages to the output image using Graphics
for (int x = 0; x < t.width; x++)
for (int y = 0; y < t.height; y++)
CroppedImage.SetPixel(x, y, atlasImage.GetPixel(t.x + x, t.y + y));
CroppedImage.Save(Path.Combine(workingFolder, t.name + ".png"), ImageFormat.Png);
}
catch (Exception ex)
{
// handle the exception
}
}
其他回答
你可以使用[图形。使用DrawImage][1]将裁剪图像从位图绘制到图形对象上。
Rectangle cropRect = new Rectangle(...);
using (Bitmap src = Image.FromFile("") as Bitmap)
{
using (Bitmap target = new Bitmap(cropRect.Width, cropRect.Height))
{
using (Graphics g = Graphics.FromImage(target))
{
g.DrawImage(src, new Rectangle(0, 0, target.Width, target.Height),
cropRect,
GraphicsUnit.Pixel);
}
}
}
在c#中裁剪图像非常简单。然而,做的东西,你将如何管理裁剪你的图像将会有点困难。
下面的示例是如何在c#中裁剪图像的方法。
var filename = @"c:\personal\images\horizon.png";
var img = Image.FromFile(filename);
var rect = new Rectangle(new Point(0, 0), img.Size);
var cloned = new Bitmap(img).Clone(rect, img.PixelFormat);
var bitmap = new Bitmap(cloned, new Size(50, 50));
cloned.Dispose();
我正在寻找一个简单和快速的函数,没有额外的库来做这项工作。我尝试了尼克斯解决方案,但我需要29.4秒来“提取”一个atlas文件的1195张图像。所以后来我用这种方法做了同样的工作,需要2.43秒。也许这个会有帮助。
// content of the Texture class
public class Texture
{
//name of the texture
public string name { get; set; }
//x position of the texture in the atlas image
public int x { get; set; }
//y position of the texture in the atlas image
public int y { get; set; }
//width of the texture in the atlas image
public int width { get; set; }
//height of the texture in the atlas image
public int height { get; set; }
}
Bitmap atlasImage = new Bitmap(@"C:\somepicture.png");
PixelFormat pixelFormat = atlasImage.PixelFormat;
foreach (Texture t in textureList)
{
try
{
CroppedImage = new Bitmap(t.width, t.height, pixelFormat);
// copy pixels over to avoid antialiasing or any other side effects of drawing
// the subimages to the output image using Graphics
for (int x = 0; x < t.width; x++)
for (int y = 0; y < t.height; y++)
CroppedImage.SetPixel(x, y, atlasImage.GetPixel(t.x + x, t.y + y));
CroppedImage.Save(Path.Combine(workingFolder, t.name + ".png"), ImageFormat.Png);
}
catch (Exception ex)
{
// handle the exception
}
}
这是另一种方法。在我的情况下,我有:
2个数字上下控制(称为LeftMargin和TopMargin) 1图片盒(pictureBox1) 1个按钮,我称之为generate C:\imagenes\myImage.gif
在按钮内部,我有这样的代码:
Image myImage = Image.FromFile(@"C:\imagenes\myImage.gif");
Bitmap croppedBitmap = new Bitmap(myImage);
croppedBitmap = croppedBitmap.Clone(
new Rectangle(
(int)LeftMargin.Value, (int)TopMargin.Value,
myImage.Width - (int)LeftMargin.Value,
myImage.Height - (int)TopMargin.Value),
System.Drawing.Imaging.PixelFormat.DontCare);
pictureBox1.Image = croppedBitmap;
我尝试在Visual studio 2012使用c#。我从这页上找到了答案
如果你在使用AForge。NET:
using(var croppedBitmap = new Crop(new Rectangle(10, 10, 10, 10)).Apply(bitmap))
{
// ...
}