1

我在silverlight中编写了一个应用程序,我在图像上放置了一个矩形,并希望选择矩形覆盖的图像部分,并在单击按钮时将其显示在图像控件上。

我不擅长处理比率和图像处理,所以我无法正确处理。

相同的代码如下所示,如果有人能建议我解决这个问题的方法或解决方案,我将不胜感激。

public void CaptureImage(object sender, RoutedEventArgs e)
{
            BitmapImage bitmapImage = new BitmapImage();
            //// bitmapImage.CreateOptions = BitmapCreateOptions.None;
            bitmapImage = NewImage;

            ////calculate bounding box
            int originalWidth = bitmapImage.PixelWidth;
            int originalHeight = bitmapImage.PixelHeight;

            int newSmallWidth = (int)SquareBlue.Width;
            int newSmallHeight = (int)SquareBlue.Height;

            ////generate temporary control to render image
            Image temporaryImage = new Image { Source = bitmapImage, Width = newSmallWidth, Height = newSmallHeight };

            ////create writeablebitmap
            WriteableBitmap wb = new WriteableBitmap(newSmallWidth, newSmallHeight);

            TranslateTransform t = new TranslateTransform();
            t.X = -5;
            t.Y = -5;

            wb.Render(temporaryImage, t);

            wb.Invalidate();

            myImage.Source = wb;
   }

每当执行此代码时,都会捕捉整个图像,而不是矩形选择的部分。谁能指导我在这里做错了什么。

4

1 回答 1

0

我建议您使用WriteableBitmapEx 库提供的 Crop 方法。

于 2013-10-08T11:18:49.563 回答