如何裁剪图像并保存到WPF中的ImageSource?

时间:2012-09-04 06:30:14

标签: wpf image crop

我是WPF的新学员。我有一个问题。 我有一个图像,宽度:360,高度:360。在这里,我想裁剪如下图像:

(0,0)到(120,120)保存到第一个ImageSource对象

(120,0)到(240,120)保存到第二个ImageSource对象,

(240,0)到(360,120)保存到第三个ImageSource对象;

...

请参阅下图中的更多详细信息: enter image description here

我的代码示例如下:

    private void CutImage(string img)
    {
        int iLeft = 0;
        int iTop = 0;
        int count = 0;

        Image thisImg = new Image();
        BitmapImage src = new BitmapImage();
        src.BeginInit();
        src.UriSource = new Uri(img, UriKind.Relative);
        src.CacheOption = BitmapCacheOption.OnLoad;
        src.EndInit();
        thisImg.Source = src;

        for (int i = 0; i < 3; i++)
        {
            iTop = i * 120;
            for (int j = 0; j < 3; j++)
            {
                iLeft = j * 120;

                Canvas canvas = new Canvas();

                Rectangle destRect = new Rectangle();
                destRect.SetValue(Canvas.LeftProperty, (double)0);
                destRect.SetValue(Canvas.TopProperty,(double)0);
                destRect.Width = destRect.Height = 120;

                Rect srcRect = new Rect();
                srcRect.X = iLeft;
                srcRect.Y = iTop;
                srcRect.Width = srcRect.Height = 120;


                thisImg.Clip = new RectangleGeometry(srcRect);

                thisImg.Clip.SetValue(Canvas.TopProperty, (double)iTop);
                thisImg.Clip.SetValue(Canvas.LeftProperty, (double)iLeft);
                thisImg.Clip.SetValue(Canvas.WidthProperty, (double)120);
                thisImg.Clip.SetValue(Canvas.HeightProperty,(double)120);

                objImg[count++] = (ImageSource)thisImg.GetValue(Image.SourceProperty);
            }
        }
      }

但它没有像我预期的那样工作,似乎所有的ImageSource对象都存储相同的图像,而不是corping部分。任何人都可以帮助我吗? THX。

2 个答案:

答案 0 :(得分:15)

使用CroppedBitmap执行此操作:

private void CutImage(string img)
{
    int count = 0;

    BitmapImage src = new BitmapImage();
    src.BeginInit();
    src.UriSource = new Uri(img, UriKind.Relative);
    src.CacheOption = BitmapCacheOption.OnLoad;
    src.EndInit();

    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
            objImg[count++] = new CroppedBitmap(src, new Int32Rect(j * 120, i * 120, 120, 120));
}

答案 1 :(得分:1)

只是做:

private static BitmapSource CaptureScreen(Visual target, double dpiX, double dpiY)
{
    if (target == null)
    {
        return null;
    }
    Rect bounds = VisualTreeHelper.GetDescendantBounds(target);
    RenderTargetBitmap rtb = new RenderTargetBitmap((int)(bounds.Width * dpiX / 96.0),
                                                    (int)(bounds.Height * dpiY / 96.0),
                                                    dpiX,
                                                    dpiY,
                                                    PixelFormats.Pbgra32);
    DrawingVisual dv = new DrawingVisual();
    using (DrawingContext ctx = dv.RenderOpen())
    {
        VisualBrush vb = new VisualBrush(target);
        ctx.DrawRectangle(vb, null, new Rect(new Point(), bounds.Size));
    }
    rtb.Render(dv);
    return rtb;
}

VisualBrush part1 = new VisualBrush(yourVISUAL);
part1.ViewBoxUnits = ..Absolute;
part1.ViewBox = new Rect(x, y, width, height);
BitmapSource bitmapSource = CaptureScreen(part1, 96, 96);
    using (var fileStream = new FileStream(filePath, FileMode.Create))
    {
        BitmapEncoder encoder = new PngBitmapEncoder();
        encoder.Frames.Add(BitmapFrame.Create(bitmapSource));
        encoder.Save(fileStream);
    }