Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用裁剪的位图源替换图像_C#_Wpf_Image_Xaml - Fatal编程技术网

C# 用裁剪的位图源替换图像

C# 用裁剪的位图源替换图像,c#,wpf,image,xaml,C#,Wpf,Image,Xaml,我的wpf控件上有一个图像 我正在尝试生成它的交叉部分-这或多或少是可以的。 我使用了codeproject解决方案来生成croped图像的BitmapSource,但当我尝试 用生成的位图源替换当前图像,如下所示 imgCurrent.Source = generatedBitmapSource; 我看到非常奇怪的行为 我需要一个建议,如何改变当前图像与新的基于位图源 我的XAMLthere没有什么特别之处-右键单击我正试图用croped替换currentImage: <DockPa

我的wpf控件上有一个图像 我正在尝试生成它的交叉部分-这或多或少是可以的。 我使用了codeproject解决方案来生成croped图像的BitmapSource,但当我尝试 用生成的位图源替换当前图像,如下所示

imgCurrent.Source = generatedBitmapSource; 
我看到非常奇怪的行为 我需要一个建议,如何改变当前图像与新的基于位图源

我的XAMLthere没有什么特别之处-右键单击我正试图用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> 
codeproject中的croping方法:

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);
        }

您确定您的Cropped图像的BitmapSource没有问题吗?您是否可以出于测试目的将其替换为另一个有效的位图,并尝试一下它是否有效。如果它与另一个兼容,但与croped image的BitmapSource不兼容,则可能您在创建croped image的BitmapSource时遇到问题。

您能描述一下您遇到的奇怪行为吗?您能显示您的裁剪和右键单击方法吗?例如,黑色方块
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);
        }