C#图像剪切,拆分,节约 [英] C# images cropping,splitting,saving

查看:427
本文介绍了C#图像剪切,拆分,节约的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在主题所述,我有一个形象:

 私人图像检测; 
测试=新位图(@sampleimg.jpg);



我想它拆分成3×3矩阵共9意图像和保存it.Any提示或技巧做这个简单吗?我使用视觉工作室2008年和智能设备的工作。尝试了一些方法,但我不能得到它。这是我尝试:

  INT X = 0; 
INT Y = 0;
INT宽度= 3;
INT高度= 3;


诠释计数= testing.Width /宽度;
BMP位图=新位图(宽,高);


图形G = Graphics.FromImage(BMP);


的for(int i = 0; I<计数;我++)
{
g.Clear(Color.Transparent);
g.DrawImage(测试,新的Rectangle(0,0,宽度,高度),新的Rectangle(X,Y,宽,高),GraphicsUnit.Pixel);
bmp.Save(Path.ChangeExtension(@C\AndrewPictures\的String.Format({0} .BMP,I)));
X + =宽度;
}


解决方案

根据不同的.NET版本,你可以做以下作物之一:



。NET 2.0

 私有静态图像cropImage(图片IMG,矩形cropArea)
{
位图bmpImage =新位图(IMG);
位图bmpCrop = bmpImage.Clone(cropArea,
bmpImage.PixelFormat);
回报(图)(bmpCrop);
}

。NET 3.5 +

  //创建一个图像元素。 
图像croppedImage =新的图像();
croppedImage.Width = 200;
croppedImage.Margin =新厚度(5);

//创建一个基于掀起了XAML定义资源的CroppedBitmap。
CroppedBitmap CB =新CroppedBitmap(
(的BitmapSource)this.Resources [MASTERIMAGE],
新Int32Rect(30,20,105,50)); //选择矩形区域
croppedImage.Source = CB; //设置图像源裁剪



正如你所看到的,这是一个有点比简单的你重新做。第一示例克隆当前图像和采用它的一个子集;第二个例子中使用 CroppedBitmap ,它支持直接从构造函数取图像的某一部分。



分裂部分很简单的数学,只分裂图像分成9组坐标并将它们传递到构造。


as stated in subject, i have an image:

    private Image testing;
    testing = new Bitmap(@"sampleimg.jpg");

I would like to split it into 3 x 3 matrix meaning 9 images in total and save it.Any tips or tricks to do this simple? I'm using visual studios 2008 and working on smart devices. Tried some ways but i can't get it. This is what i tried:

        int x = 0;
        int y = 0;
        int width = 3;
        int height = 3;


        int count = testing.Width / width;
        Bitmap bmp = new Bitmap(width, height);


        Graphics g = Graphics.FromImage(bmp);


        for (int i = 0; i < count; i++)
        {
            g.Clear(Color.Transparent);
            g.DrawImage(testing, new Rectangle(0, 0, width, height), new Rectangle(x, y, width, height), GraphicsUnit.Pixel);
            bmp.Save(Path.ChangeExtension(@"C\AndrewPictures\", String.Format(".{0}.bmp",i)));
            x += width;
        } 

解决方案

Depending on the .NET version, you could do one of the following to crop:

.NET 2.0

private static Image cropImage(Image img, Rectangle cropArea)
{
   Bitmap bmpImage = new Bitmap(img);
   Bitmap bmpCrop = bmpImage.Clone(cropArea,
   bmpImage.PixelFormat);
   return (Image)(bmpCrop);
}

Or .NET 3.5+

// Create an Image element.
Image croppedImage = new Image();
croppedImage.Width = 200;
croppedImage.Margin = new Thickness(5);

// Create a CroppedBitmap based off of a xaml defined resource.
CroppedBitmap cb = new CroppedBitmap(     
   (BitmapSource)this.Resources["masterImage"],
   new Int32Rect(30, 20, 105, 50));       //select region rect
croppedImage.Source = cb;                 //set image source to cropped

As you can see, it's a bit more simple than what you're doing. The first example clones the current image and takes a subset of it; the second example uses CroppedBitmap, which supports taking a section of the image right from the constructor.

The splitting part is simple maths, just splitting the image into 9 sets of coordinates and passing them into the constructor.

这篇关于C#图像剪切,拆分,节约的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆