Note that there are some explanatory texts on larger screens.

plurals
  1. POReplace Image with cropped BitmapSource
    primarykey
    data
    text
    <p>I have an Image on my wpf control and I am trying to generate croped part of it - this is ok more or less. I have used a codeproject solution to generate BitmapSource of croped image (<a href="http://www.codeproject.com/KB/WPF/CropAdorner.aspx" rel="nofollow">http://www.codeproject.com/KB/WPF/CropAdorner.aspx</a>) but when I am trying to replace current image with generated BitmapSource like this</p> <pre><code>imgCurrent.Source = generatedBitmapSource; </code></pre> <p>I see very strange behaviour (( I need an advice how to change current Image with new based on BitmapSource.</p> <p>my XAML(there is nothing extraordinary - and by the right click I am trying to replace currentImage with croped):</p> <pre><code>&lt;DockPanel Height="395" Width="926"&gt; &lt;!--Went with a DockPanel here so that the image would always be centered in its parent control.--&gt; &lt;Image x:Name="imgCurrent" VerticalAlignment="Center" HorizontalAlignment="Center" MouseRightButtonDown="imgCurrent_MouseRightButtonDown"/&gt; &lt;/DockPanel&gt; </code></pre> <p>right click:</p> <pre><code> 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; } </code></pre> <p>croping method (from codeproject):</p> <pre><code>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); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload