简体   繁体   English

用裁剪后的BitmapSource替换图像

[英]Replace Image with cropped BitmapSource

I have an Image on my wpf control and I am trying to generate croped part of it - this is ok more or less. 我的wpf控件上有一个Image,我正在尝试生成其中的裁剪部分-或多或少都可以。 I have used a codeproject solution to generate BitmapSource of croped image ( http://www.codeproject.com/KB/WPF/CropAdorner.aspx ) but when I am trying to replace current image with generated BitmapSource like this 我使用了一个codeproject解决方案来生成裁剪图像的BitmapSource( http://www.codeproject.com/KB/WPF/CropAdorner.aspx ),但是当我尝试用生成的BitmapSource替换当前图像时

imgCurrent.Source = generatedBitmapSource; 

I see very strange behaviour (( I need an advice how to change current Image with new based on BitmapSource. 我看到了非常奇怪的行为((我需要一个建议,如何基于BitmapSource使用新的更改当前Image。

my XAML(there is nothing extraordinary - and by the right click I am trying to replace currentImage with croped): 我的XAML(没有什么特别的-通过右键单击,我尝试用croped替换currentImage):

<DockPanel Height="395" Width="926">
    <!--Went with a DockPanel here so that the image would always be centered in its parent control.-->
    <Image x:Name="imgCurrent" VerticalAlignment="Center" HorizontalAlignment="Center" MouseRightButtonDown="imgCurrent_MouseRightButtonDown"/>
</DockPanel> 

right click: 右键点击:

  private void imgCurrent_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
    { 
           generatedBitmapSource = _clp.BpsCrop();
           //this clears croping adonder
           AdornerLayer aly = AdornerLayer.GetAdornerLayer(_felCur);
           aly.Remove(_clp);
    //
       imageCurrent.Source = generatedBitmapSource;
    }

croping method (from codeproject): 裁剪方法(来自codeproject):

public BitmapSource BpsCrop()
        {
            Thickness margin = AdornerMargin();
            Rect rcInterior = _prCropMask.RectInterior;

            Point pxFromSize = UnitsToPx(rcInterior.Width, rcInterior.Height);

            // It appears that CroppedBitmap indexes from the upper left of the margin whereas RenderTargetBitmap renders the
            // control exclusive of the margin.  Hence our need to take the margins into account here...

            Point pxFromPos = UnitsToPx(rcInterior.Left + margin.Left, rcInterior.Top + margin.Top);
            Point pxWhole = UnitsToPx(AdornedElement.RenderSize.Width + margin.Left, AdornedElement.RenderSize.Height + margin.Left);
            pxFromSize.X = Math.Max(Math.Min(pxWhole.X - pxFromPos.X, pxFromSize.X), 0);
            pxFromSize.Y = Math.Max(Math.Min(pxWhole.Y - pxFromPos.Y, pxFromSize.Y), 0);
            if (pxFromSize.X == 0 || pxFromSize.Y == 0)
            {
                return null;
            }
            System.Windows.Int32Rect rcFrom = new System.Windows.Int32Rect(pxFromPos.X, pxFromPos.Y, pxFromSize.X, pxFromSize.Y);

            RenderTargetBitmap rtb = new RenderTargetBitmap(pxWhole.X, pxWhole.Y, s_dpiX, s_dpiY, PixelFormats.Default);
            rtb.Render(AdornedElement);
            return new CroppedBitmap(rtb, rcFrom);
        }

Are you sure you don't have issue with your "BitmapSource of croped image"? 您确定“裁剪图像的BitmapSource”没有问题吗? Can you for test purpose replace it with another valid Bitmap and try if it works. 您是否可以出于测试目的将其替换为另一个有效的位图,然后尝试是否可行。 If it works with another one, but not "BitmapSource of croped image" then maybe you have issue with creating "BitmapSource of croped image". 如果它与另一种兼容,但不适用于“裁剪图像的BitmapSource”,则可能与创建“裁剪图像的BitmapSource”有关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM