@ -54,11 +54,7 @@ public class AdvancedImageBox : TemplatedControl
remove = > _ propertyChanged - = value ;
remove = > _ propertyChanged - = value ;
}
}
protected bool RaiseAndSetIfChanged < T > (
protected bool RaiseAndSetIfChanged < T > ( ref T field , T value , [ CallerMemberName ] string? propertyName = null )
ref T field ,
T value ,
[CallerMemberName] string? propertyName = null
)
{
{
if ( EqualityComparer < T > . Default . Equals ( field , value ) )
if ( EqualityComparer < T > . Default . Equals ( field , value ) )
return false ;
return false ;
@ -335,9 +331,7 @@ public class AdvancedImageBox : TemplatedControl
if ( index < Count - 1 )
if ( index < Count - 1 )
index + + ;
index + + ;
return constrainZoomLevel > 0 & & this [ index ] > = constrainZoomLevel
return constrainZoomLevel > 0 & & this [ index ] > = constrainZoomLevel ? constrainZoomLevel : this [ index ] ;
? constrainZoomLevel
: this [ index ] ;
}
}
/// <summary>
/// <summary>
@ -352,9 +346,7 @@ public class AdvancedImageBox : TemplatedControl
if ( index > 0 )
if ( index > 0 )
index - - ;
index - - ;
return constrainZoomLevel > 0 & & this [ index ] < = constrainZoomLevel
return constrainZoomLevel > 0 & & this [ index ] < = constrainZoomLevel ? constrainZoomLevel : this [ index ] ;
? constrainZoomLevel
: this [ index ] ;
}
}
/// <summary>
/// <summary>
@ -525,11 +517,10 @@ public class AdvancedImageBox : TemplatedControl
# endregion
# endregion
#region Properties
#region Properties
public static readonly DirectProperty < AdvancedImageBox , bool > CanRenderProperty =
public static readonly DirectProperty < AdvancedImageBox , bool > CanRenderProperty = AvaloniaProperty . RegisterDirect <
AvaloniaProperty . RegisterDirect < AdvancedImageBox , bool > (
AdvancedImageBox ,
nameof ( CanRender ) ,
bool
o = > o . CanRender
> ( nameof ( CanRender ) , o = > o . CanRender ) ;
) ;
/// <summary>
/// <summary>
/// Gets or sets if control can render the image
/// Gets or sets if control can render the image
@ -560,11 +551,10 @@ public class AdvancedImageBox : TemplatedControl
set = > SetValue ( GridCellSizeProperty , value ) ;
set = > SetValue ( GridCellSizeProperty , value ) ;
}
}
public static readonly StyledProperty < ISolidColorBrush > GridColorProperty =
public static readonly StyledProperty < ISolidColorBrush > GridColorProperty = AvaloniaProperty . Register <
AvaloniaProperty . Register < AdvancedImageBox , ISolidColorBrush > (
AdvancedImageBox ,
nameof ( GridColor ) ,
ISolidColorBrush
SolidColorBrush . Parse ( "#181818" )
> ( nameof ( GridColor ) , SolidColorBrush . Parse ( "#181818" ) ) ;
) ;
/// <summary>
/// <summary>
/// Gets or sets the color used to create the checkerboard style background
/// Gets or sets the color used to create the checkerboard style background
@ -575,11 +565,10 @@ public class AdvancedImageBox : TemplatedControl
set = > SetValue ( GridColorProperty , value ) ;
set = > SetValue ( GridColorProperty , value ) ;
}
}
public static readonly StyledProperty < ISolidColorBrush > GridColorAlternateProperty =
public static readonly StyledProperty < ISolidColorBrush > GridColorAlternateProperty = AvaloniaProperty . Register <
AvaloniaProperty . Register < AdvancedImageBox , ISolidColorBrush > (
AdvancedImageBox ,
nameof ( GridColorAlternate ) ,
ISolidColorBrush
SolidColorBrush . Parse ( "#252525" )
> ( nameof ( GridColorAlternate ) , SolidColorBrush . Parse ( "#252525" ) ) ;
) ;
/// <summary>
/// <summary>
/// Gets or sets the color used to create the checkerboard style background
/// Gets or sets the color used to create the checkerboard style background
@ -606,7 +595,8 @@ public class AdvancedImageBox : TemplatedControl
{
{
var loader = ImageLoader . AsyncImageLoader ;
var loader = ImageLoader . AsyncImageLoader ;
Dispatcher . UIThread
Dispatcher
. UIThread
. InvokeAsync ( async ( ) = >
. InvokeAsync ( async ( ) = >
{
{
Image = await loader . ProvideImageAsync ( value ) ;
Image = await loader . ProvideImageAsync ( value ) ;
@ -616,10 +606,9 @@ public class AdvancedImageBox : TemplatedControl
}
}
}
}
public static readonly StyledProperty < Bitmap ? > ImageProperty = AvaloniaProperty . Register <
public static readonly StyledProperty < Bitmap ? > ImageProperty = AvaloniaProperty . Register < AdvancedImageBox , Bitmap ? > (
AdvancedImageBox ,
nameof ( Image )
Bitmap ?
) ;
> ( nameof ( Image ) ) ;
/// <summary>
/// <summary>
/// Gets or sets the image to be displayed
/// Gets or sets the image to be displayed
@ -656,8 +645,7 @@ public class AdvancedImageBox : TemplatedControl
{
{
offsetBackup = Offset ;
offsetBackup = Offset ;
var zoomFactorScale =
var zoomFactorScale = ( float ) GetZoomLevelToFit ( newImage ) / GetZoomLevelToFit ( oldImage ) ;
( float ) GetZoomLevelToFit ( newImage ) / GetZoomLevelToFit ( oldImage ) ;
var imageScale = newImage . Size / oldImage . Size ;
var imageScale = newImage . Size / oldImage . Size ;
Debug . WriteLine ( $"Image scale: {imageScale}" ) ;
Debug . WriteLine ( $"Image scale: {imageScale}" ) ;
@ -731,8 +719,10 @@ public class AdvancedImageBox : TemplatedControl
public bool HaveTrackerImage = > _ trackerImage is not null ;
public bool HaveTrackerImage = > _ trackerImage is not null ;
public static readonly StyledProperty < bool > TrackerImageAutoZoomProperty =
public static readonly StyledProperty < bool > TrackerImageAutoZoomProperty = AvaloniaProperty . Register <
AvaloniaProperty . Register < AdvancedImageBox , bool > ( nameof ( TrackerImageAutoZoom ) , true ) ;
AdvancedImageBox ,
bool
> ( nameof ( TrackerImageAutoZoom ) , true ) ;
/// <summary>
/// <summary>
/// Gets or sets if the tracker image will be scaled to the current zoom
/// Gets or sets if the tracker image will be scaled to the current zoom
@ -743,8 +733,10 @@ public class AdvancedImageBox : TemplatedControl
set = > SetValue ( TrackerImageAutoZoomProperty , value ) ;
set = > SetValue ( TrackerImageAutoZoomProperty , value ) ;
}
}
public static readonly StyledProperty < bool > IsTrackerImageEnabledProperty =
public static readonly StyledProperty < bool > IsTrackerImageEnabledProperty = AvaloniaProperty . Register <
AvaloniaProperty . Register < AdvancedImageBox , bool > ( "IsTrackerImageEnabled" ) ;
AdvancedImageBox ,
bool
> ( "IsTrackerImageEnabled" ) ;
public bool IsTrackerImageEnabled
public bool IsTrackerImageEnabled
{
{
@ -776,10 +768,10 @@ public class AdvancedImageBox : TemplatedControl
}
}
}
}
public static readonly StyledProperty < bool > ShowGridProperty = AvaloniaProperty . Register <
public static readonly StyledProperty < bool > ShowGridProperty = AvaloniaProperty . Register < AdvancedImageBox , bool > (
AdvancedImageBox ,
nameof ( ShowGrid ) ,
bool
true
> ( nameof ( ShowGrid ) , true ) ;
) ;
/// <summary>
/// <summary>
/// Gets or sets the grid visibility when reach high zoom levels
/// Gets or sets the grid visibility when reach high zoom levels
@ -791,10 +783,7 @@ public class AdvancedImageBox : TemplatedControl
}
}
public static readonly DirectProperty < AdvancedImageBox , Point > PointerPositionProperty =
public static readonly DirectProperty < AdvancedImageBox , Point > PointerPositionProperty =
AvaloniaProperty . RegisterDirect < AdvancedImageBox , Point > (
AvaloniaProperty . RegisterDirect < AdvancedImageBox , Point > ( nameof ( PointerPosition ) , o = > o . PointerPosition ) ;
nameof ( PointerPosition ) ,
o = > o . PointerPosition
) ;
/// <summary>
/// <summary>
/// Gets the current pointer position
/// Gets the current pointer position
@ -805,11 +794,10 @@ public class AdvancedImageBox : TemplatedControl
private set = > SetAndRaise ( PointerPositionProperty , ref _ pointerPosition , value ) ;
private set = > SetAndRaise ( PointerPositionProperty , ref _ pointerPosition , value ) ;
}
}
public static readonly DirectProperty < AdvancedImageBox , bool > IsPanningProperty =
public static readonly DirectProperty < AdvancedImageBox , bool > IsPanningProperty = AvaloniaProperty . RegisterDirect <
AvaloniaProperty . RegisterDirect < AdvancedImageBox , bool > (
AdvancedImageBox ,
nameof ( IsPanning ) ,
bool
o = > o . IsPanning
> ( nameof ( IsPanning ) , o = > o . IsPanning ) ;
) ;
/// <summary>
/// <summary>
/// Gets if control is currently panning
/// Gets if control is currently panning
@ -836,11 +824,10 @@ public class AdvancedImageBox : TemplatedControl
}
}
}
}
public static readonly DirectProperty < AdvancedImageBox , bool > IsSelectingProperty =
public static readonly DirectProperty < AdvancedImageBox , bool > IsSelectingProperty = AvaloniaProperty . RegisterDirect <
AvaloniaProperty . RegisterDirect < AdvancedImageBox , bool > (
AdvancedImageBox ,
nameof ( IsSelecting ) ,
bool
o = > o . IsSelecting
> ( nameof ( IsSelecting ) , o = > o . IsSelecting ) ;
) ;
/// <summary>
/// <summary>
/// Gets if control is currently selecting a ROI
/// Gets if control is currently selecting a ROI
@ -863,10 +850,10 @@ public class AdvancedImageBox : TemplatedControl
}
}
}
}
public static readonly StyledProperty < bool > AutoPanProperty = AvaloniaProperty . Register <
public static readonly StyledProperty < bool > AutoPanProperty = AvaloniaProperty . Register < AdvancedImageBox , bool > (
AdvancedImageBox ,
nameof ( AutoPan ) ,
bool
true
> ( nameof ( AutoPan ) , true ) ;
) ;
/// <summary>
/// <summary>
/// Gets or sets if the control can pan with the mouse
/// Gets or sets if the control can pan with the mouse
@ -877,11 +864,10 @@ public class AdvancedImageBox : TemplatedControl
set = > SetValue ( AutoPanProperty , value ) ;
set = > SetValue ( AutoPanProperty , value ) ;
}
}
public static readonly StyledProperty < MouseButtons > PanWithMouseButtonsProperty =
public static readonly StyledProperty < MouseButtons > PanWithMouseButtonsProperty = AvaloniaProperty . Register <
AvaloniaProperty . Register < AdvancedImageBox , MouseButtons > (
AdvancedImageBox ,
nameof ( PanWithMouseButtons ) ,
MouseButtons
MouseButtons . LeftButton | MouseButtons . MiddleButton
> ( nameof ( PanWithMouseButtons ) , MouseButtons . LeftButton | MouseButtons . MiddleButton ) ;
) ;
/// <summary>
/// <summary>
/// Gets or sets the mouse buttons to pan the image
/// Gets or sets the mouse buttons to pan the image
@ -906,11 +892,10 @@ public class AdvancedImageBox : TemplatedControl
set = > SetValue ( PanWithArrowsProperty , value ) ;
set = > SetValue ( PanWithArrowsProperty , value ) ;
}
}
public static readonly StyledProperty < MouseButtons > SelectWithMouseButtonsProperty =
public static readonly StyledProperty < MouseButtons > SelectWithMouseButtonsProperty = AvaloniaProperty . Register <
AvaloniaProperty . Register < AdvancedImageBox , MouseButtons > (
AdvancedImageBox ,
nameof ( SelectWithMouseButtons ) ,
MouseButtons
MouseButtons . LeftButton
> ( nameof ( SelectWithMouseButtons ) , MouseButtons . LeftButton ) ;
) ;
/// <summary>
/// <summary>
/// Gets or sets the mouse buttons to select a region on image
/// Gets or sets the mouse buttons to select a region on image
@ -935,10 +920,10 @@ public class AdvancedImageBox : TemplatedControl
set = > SetValue ( InvertMousePanProperty , value ) ;
set = > SetValue ( InvertMousePanProperty , value ) ;
}
}
public static readonly StyledProperty < bool > AutoCenterProperty = AvaloniaProperty . Register <
public static readonly StyledProperty < bool > AutoCenterProperty = AvaloniaProperty . Register < AdvancedImageBox , bool > (
AdvancedImageBox ,
nameof ( AutoCenter ) ,
bool
true
> ( nameof ( AutoCenter ) , true ) ;
) ;
/// <summary>
/// <summary>
/// Gets or sets if image is auto centered
/// Gets or sets if image is auto centered
@ -993,10 +978,10 @@ public class AdvancedImageBox : TemplatedControl
}
}
}
}
public static readonly StyledProperty < bool > AllowZoomProperty = AvaloniaProperty . Register <
public static readonly StyledProperty < bool > AllowZoomProperty = AvaloniaProperty . Register < AdvancedImageBox , bool > (
AdvancedImageBox ,
nameof ( AllowZoom ) ,
bool
true
> ( nameof ( AllowZoom ) , true ) ;
) ;
/// <summary>
/// <summary>
/// Gets or sets if zoom is allowed
/// Gets or sets if zoom is allowed
@ -1007,10 +992,8 @@ public class AdvancedImageBox : TemplatedControl
set = > SetValue ( AllowZoomProperty , value ) ;
set = > SetValue ( AllowZoomProperty , value ) ;
}
}
public static readonly DirectProperty <
public static readonly DirectProperty < AdvancedImageBox , ZoomLevelCollection > ZoomLevelsProperty =
AdvancedImageBox ,
AvaloniaProperty . RegisterDirect < AdvancedImageBox , ZoomLevelCollection > (
ZoomLevelCollection
> ZoomLevelsProperty = AvaloniaProperty . RegisterDirect < AdvancedImageBox , ZoomLevelCollection > (
nameof ( ZoomLevels ) ,
nameof ( ZoomLevels ) ,
o = > o . ZoomLevels ,
o = > o . ZoomLevels ,
( o , v ) = > o . ZoomLevels = v
( o , v ) = > o . ZoomLevels = v
@ -1028,10 +1011,10 @@ public class AdvancedImageBox : TemplatedControl
set = > SetAndRaise ( ZoomLevelsProperty , ref _ zoomLevels , value ) ;
set = > SetAndRaise ( ZoomLevelsProperty , ref _ zoomLevels , value ) ;
}
}
public static readonly StyledProperty < int > MinZoomProperty = AvaloniaProperty . Register <
public static readonly StyledProperty < int > MinZoomProperty = AvaloniaProperty . Register < AdvancedImageBox , int > (
AdvancedImageBox ,
nameof ( MinZoom ) ,
int
1 0
> ( nameof ( MinZoom ) , 1 0 ) ;
) ;
/// <summary>
/// <summary>
/// Gets or sets the minimum possible zoom.
/// Gets or sets the minimum possible zoom.
@ -1043,10 +1026,10 @@ public class AdvancedImageBox : TemplatedControl
set = > SetValue ( MinZoomProperty , value ) ;
set = > SetValue ( MinZoomProperty , value ) ;
}
}
public static readonly StyledProperty < int > MaxZoomProperty = AvaloniaProperty . Register <
public static readonly StyledProperty < int > MaxZoomProperty = AvaloniaProperty . Register < AdvancedImageBox , int > (
AdvancedImageBox ,
nameof ( MaxZoom ) ,
int
6 4 0 0
> ( nameof ( MaxZoom ) , 6 4 0 0 ) ;
) ;
/// <summary>
/// <summary>
/// Gets or sets the maximum possible zoom.
/// Gets or sets the maximum possible zoom.
@ -1058,8 +1041,10 @@ public class AdvancedImageBox : TemplatedControl
set = > SetValue ( MaxZoomProperty , value ) ;
set = > SetValue ( MaxZoomProperty , value ) ;
}
}
public static readonly StyledProperty < bool > ConstrainZoomOutToFitLevelProperty =
public static readonly StyledProperty < bool > ConstrainZoomOutToFitLevelProperty = AvaloniaProperty . Register <
AvaloniaProperty . Register < AdvancedImageBox , bool > ( nameof ( ConstrainZoomOutToFitLevel ) , true ) ;
AdvancedImageBox ,
bool
> ( nameof ( ConstrainZoomOutToFitLevel ) , true ) ;
/// <summary>
/// <summary>
/// Gets or sets if the zoom out should constrain to fit image as the lowest zoom level.
/// Gets or sets if the zoom out should constrain to fit image as the lowest zoom level.
@ -1070,8 +1055,10 @@ public class AdvancedImageBox : TemplatedControl
set = > SetValue ( ConstrainZoomOutToFitLevelProperty , value ) ;
set = > SetValue ( ConstrainZoomOutToFitLevelProperty , value ) ;
}
}
public static readonly DirectProperty < AdvancedImageBox , int > OldZoomProperty =
public static readonly DirectProperty < AdvancedImageBox , int > OldZoomProperty = AvaloniaProperty . RegisterDirect <
AvaloniaProperty . RegisterDirect < AdvancedImageBox , int > ( nameof ( OldZoom ) , o = > o . OldZoom ) ;
AdvancedImageBox ,
int
> ( nameof ( OldZoom ) , o = > o . OldZoom ) ;
private int _ oldZoom = 1 0 0 ;
private int _ oldZoom = 1 0 0 ;
@ -1085,10 +1072,10 @@ public class AdvancedImageBox : TemplatedControl
private set = > SetAndRaise ( OldZoomProperty , ref _ oldZoom , value ) ;
private set = > SetAndRaise ( OldZoomProperty , ref _ oldZoom , value ) ;
}
}
public static readonly StyledProperty < int > ZoomProperty = AvaloniaProperty . Register <
public static readonly StyledProperty < int > ZoomProperty = AvaloniaProperty . Register < AdvancedImageBox , int > (
AdvancedImageBox ,
nameof ( Zoom ) ,
int
1 0 0
> ( nameof ( Zoom ) , 1 0 0 ) ;
) ;
/// <summary>
/// <summary>
/// Gets or sets the zoom.
/// Gets or sets the zoom.
@ -1178,11 +1165,10 @@ public class AdvancedImageBox : TemplatedControl
/// <value>The height of the scaled image.</value>
/// <value>The height of the scaled image.</value>
public double ScaledImageHeight = > Image ? . Size . Height * ZoomFactor ? ? 0 ;
public double ScaledImageHeight = > Image ? . Size . Height * ZoomFactor ? ? 0 ;
public static readonly StyledProperty < ISolidColorBrush > PixelGridColorProperty =
public static readonly StyledProperty < ISolidColorBrush > PixelGridColorProperty = AvaloniaProperty . Register <
AvaloniaProperty . Register < AdvancedImageBox , ISolidColorBrush > (
AdvancedImageBox ,
nameof ( PixelGridColor ) ,
ISolidColorBrush
Brushes . DimGray
> ( nameof ( PixelGridColor ) , Brushes . DimGray ) ;
) ;
/// <summary>
/// <summary>
/// Gets or sets the color of the pixel grid.
/// Gets or sets the color of the pixel grid.
@ -1194,8 +1180,10 @@ public class AdvancedImageBox : TemplatedControl
set = > SetValue ( PixelGridColorProperty , value ) ;
set = > SetValue ( PixelGridColorProperty , value ) ;
}
}
public static readonly StyledProperty < int > PixelGridZoomThresholdProperty =
public static readonly StyledProperty < int > PixelGridZoomThresholdProperty = AvaloniaProperty . Register <
AvaloniaProperty . Register < AdvancedImageBox , int > ( nameof ( PixelGridZoomThreshold ) , 1 3 ) ;
AdvancedImageBox ,
int
> ( nameof ( PixelGridZoomThreshold ) , 1 3 ) ;
/// <summary>
/// <summary>
/// Minimum size of zoomed pixel's before the pixel grid will be drawn
/// Minimum size of zoomed pixel's before the pixel grid will be drawn
@ -1206,11 +1194,15 @@ public class AdvancedImageBox : TemplatedControl
set = > SetValue ( PixelGridZoomThresholdProperty , value ) ;
set = > SetValue ( PixelGridZoomThresholdProperty , value ) ;
}
}
public static readonly StyledProperty < SelectionModes > SelectionModeProperty =
public static readonly StyledProperty < SelectionModes > SelectionModeProperty = AvaloniaProperty . Register <
AvaloniaProperty . Register < AdvancedImageBox , SelectionModes > ( nameof ( SelectionMode ) ) ;
AdvancedImageBox ,
SelectionModes
> ( nameof ( SelectionMode ) ) ;
public static readonly StyledProperty < bool > IsPixelGridEnabledProperty =
public static readonly StyledProperty < bool > IsPixelGridEnabledProperty = AvaloniaProperty . Register <
AvaloniaProperty . Register < AdvancedImageBox , bool > ( "IsPixelGridEnabled" , true ) ;
AdvancedImageBox ,
bool
> ( "IsPixelGridEnabled" , true ) ;
/// <summary>
/// <summary>
/// Whether or not to draw the pixel grid at the <see cref="PixelGridZoomThreshold"/>
/// Whether or not to draw the pixel grid at the <see cref="PixelGridZoomThreshold"/>
@ -1227,11 +1219,10 @@ public class AdvancedImageBox : TemplatedControl
set = > SetValue ( SelectionModeProperty , value ) ;
set = > SetValue ( SelectionModeProperty , value ) ;
}
}
public static readonly StyledProperty < ISolidColorBrush > SelectionColorProperty =
public static readonly StyledProperty < ISolidColorBrush > SelectionColorProperty = AvaloniaProperty . Register <
AvaloniaProperty . Register < AdvancedImageBox , ISolidColorBrush > (
AdvancedImageBox ,
nameof ( SelectionColor ) ,
ISolidColorBrush
new SolidColorBrush ( new Color ( 1 2 7 , 0 , 1 2 8 , 2 5 5 ) )
> ( nameof ( SelectionColor ) , new SolidColorBrush ( new Color ( 1 2 7 , 0 , 1 2 8 , 2 5 5 ) ) ) ;
) ;
public ISolidColorBrush SelectionColor
public ISolidColorBrush SelectionColor
{
{
@ -1313,14 +1304,9 @@ public class AdvancedImageBox : TemplatedControl
AffectsRender < AdvancedImageBox > ( ShowGridProperty ) ;
AffectsRender < AdvancedImageBox > ( ShowGridProperty ) ;
HorizontalScrollBar =
HorizontalScrollBar =
e . NameScope . Find < ScrollBar > ( "PART_HorizontalScrollBar" )
e . NameScope . Find < ScrollBar > ( "PART_HorizontalScrollBar" ) ? ? throw new NullReferenceException ( ) ;
? ? throw new NullReferenceException ( ) ;
VerticalScrollBar = e . NameScope . Find < ScrollBar > ( "PART_VerticalScrollBar" ) ? ? throw new NullReferenceException ( ) ;
VerticalScrollBar =
ViewPort = e . NameScope . Find < ContentPresenter > ( "PART_ViewPort" ) ? ? throw new NullReferenceException ( ) ;
e . NameScope . Find < ScrollBar > ( "PART_VerticalScrollBar" )
? ? throw new NullReferenceException ( ) ;
ViewPort =
e . NameScope . Find < ContentPresenter > ( "PART_ViewPort" )
? ? throw new NullReferenceException ( ) ;
SizeModeChanged ( ) ;
SizeModeChanged ( ) ;
@ -1347,13 +1333,12 @@ public class AdvancedImageBox : TemplatedControl
// If we're in high zoom, switch off bitmap interpolation mode
// If we're in high zoom, switch off bitmap interpolation mode
// Otherwise use high quality
// Otherwise use high quality
BitmapInterpolationMode = isHighZoom
BitmapInterpolationMode = isHighZoom ? BitmapInterpolationMode . None : BitmapInterpolationMode . HighQuality ;
? BitmapInterpolationMode . None
: BitmapInterpolationMode . HighQuality ;
InvalidateVisual ( ) ;
InvalidateVisual ( ) ;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void RenderBackgroundGrid ( DrawingContext context )
private void RenderBackgroundGrid ( DrawingContext context )
{
{
var size = GridCellSize ;
var size = GridCellSize ;
@ -1387,8 +1372,6 @@ public class AdvancedImageBox : TemplatedControl
public override void Render ( DrawingContext context )
public override void Render ( DrawingContext context )
{
{
base . Render ( context ) ;
var gridCellSize = GridCellSize ;
var gridCellSize = GridCellSize ;
if ( ShowGrid & gridCellSize > 0 & & ( ! IsHorizontalBarVisible | | ! IsVerticalBarVisible ) )
if ( ShowGrid & gridCellSize > 0 & & ( ! IsHorizontalBarVisible | | ! IsVerticalBarVisible ) )
@ -1399,9 +1382,7 @@ public class AdvancedImageBox : TemplatedControl
var zoomFactor = ZoomFactor ;
var zoomFactor = ZoomFactor ;
var shouldDrawPixelGrid =
var shouldDrawPixelGrid =
IsPixelGridEnabled
IsPixelGridEnabled & & SizeMode = = SizeModes . Normal & & zoomFactor > PixelGridZoomThreshold ;
& & SizeMode = = SizeModes . Normal
& & zoomFactor > PixelGridZoomThreshold ;
// Draw Grid
// Draw Grid
/ * var viewPortSize = ViewPortSize ;
/ * var viewPortSize = ViewPortSize ;
@ -1441,16 +1422,10 @@ public class AdvancedImageBox : TemplatedControl
if ( HaveTrackerImage & & _ pointerPosition is { X : > = 0 , Y : > = 0 } )
if ( HaveTrackerImage & & _ pointerPosition is { X : > = 0 , Y : > = 0 } )
{
{
var destSize = TrackerImageAutoZoom
var destSize = TrackerImageAutoZoom
? new Size (
? new Size ( _ trackerImage ! . Size . Width * zoomFactor , _ trackerImage . Size . Height * zoomFactor )
_ trackerImage ! . Size . Width * zoomFactor ,
_ trackerImage . Size . Height * zoomFactor
)
: image . Size ;
: image . Size ;
var destPos = new Point (
var destPos = new Point ( _ pointerPosition . X - destSize . Width / 2 , _ pointerPosition . Y - destSize . Height / 2 ) ;
_ pointerPosition . X - destSize . Width / 2 ,
_ pointerPosition . Y - destSize . Height / 2
) ;
context . DrawImage ( _ trackerImage ! , new Rect ( destPos , destSize ) ) ;
context . DrawImage ( _ trackerImage ! , new Rect ( destPos , destSize ) ) ;
}
}
@ -1462,30 +1437,14 @@ public class AdvancedImageBox : TemplatedControl
var offsetY = Offset . Y % zoomFactor ;
var offsetY = Offset . Y % zoomFactor ;
Pen pen = new ( PixelGridColor ) ;
Pen pen = new ( PixelGridColor ) ;
for (
for ( var x = imageViewPort . X + zoomFactor - offsetX ; x < imageViewPort . Right ; x + = zoomFactor )
var x = imageViewPort . X + zoomFactor - offsetX ;
x < imageViewPort . Right ;
x + = zoomFactor
)
{
{
context . DrawLine (
context . DrawLine ( pen , new Point ( x , imageViewPort . X ) , new Point ( x , imageViewPort . Bottom ) ) ;
pen ,
new Point ( x , imageViewPort . X ) ,
new Point ( x , imageViewPort . Bottom )
) ;
}
}
for (
for ( var y = imageViewPort . Y + zoomFactor - offsetY ; y < imageViewPort . Bottom ; y + = zoomFactor )
var y = imageViewPort . Y + zoomFactor - offsetY ;
y < imageViewPort . Bottom ;
y + = zoomFactor
)
{
{
context . DrawLine (
context . DrawLine ( pen , new Point ( imageViewPort . Y , y ) , new Point ( imageViewPort . Right , y ) ) ;
pen ,
new Point ( imageViewPort . Y , y ) ,
new Point ( imageViewPort . Right , y )
) ;
}
}
context . DrawRectangle ( pen , imageViewPort ) ;
context . DrawRectangle ( pen , imageViewPort ) ;
@ -1496,12 +1455,7 @@ public class AdvancedImageBox : TemplatedControl
var rect = GetOffsetRectangle ( SelectionRegion ) ;
var rect = GetOffsetRectangle ( SelectionRegion ) ;
var selectionColor = SelectionColor ;
var selectionColor = SelectionColor ;
context . FillRectangle ( selectionColor , rect ) ;
context . FillRectangle ( selectionColor , rect ) ;
var color = Color . FromArgb (
var color = Color . FromArgb ( 2 5 5 , selectionColor . Color . R , selectionColor . Color . G , selectionColor . Color . B ) ;
2 5 5 ,
selectionColor . Color . R ,
selectionColor . Color . G ,
selectionColor . Color . B
) ;
context . DrawRectangle ( new Pen ( color . ToUInt32 ( ) ) , rect ) ;
context . DrawRectangle ( new Pen ( color . ToUInt32 ( ) ) , rect ) ;
}
}
}
}
@ -1609,8 +1563,7 @@ public class AdvancedImageBox : TemplatedControl
{
{
if (
if (
! (
! (
pointer . Properties . IsLeftButtonPressed
pointer . Properties . IsLeftButtonPressed & & ( SelectWithMouseButtons & MouseButtons . LeftButton ) ! = 0
& & ( SelectWithMouseButtons & MouseButtons . LeftButton ) ! = 0
| | pointer . Properties . IsMiddleButtonPressed
| | pointer . Properties . IsMiddleButtonPressed
& & ( SelectWithMouseButtons & MouseButtons . MiddleButton ) ! = 0
& & ( SelectWithMouseButtons & MouseButtons . MiddleButton ) ! = 0
| | pointer . Properties . IsRightButtonPressed
| | pointer . Properties . IsRightButtonPressed
@ -1624,12 +1577,10 @@ public class AdvancedImageBox : TemplatedControl
{
{
if (
if (
! (
! (
pointer . Properties . IsLeftButtonPressed
pointer . Properties . IsLeftButtonPressed & & ( PanWithMouseButtons & MouseButtons . LeftButton ) ! = 0
& & ( PanWithMouseButtons & MouseButtons . LeftButton ) ! = 0
| | pointer . Properties . IsMiddleButtonPressed
| | pointer . Properties . IsMiddleButtonPressed
& & ( PanWithMouseButtons & MouseButtons . MiddleButton ) ! = 0
& & ( PanWithMouseButtons & MouseButtons . MiddleButton ) ! = 0
| | pointer . Properties . IsRightButtonPressed
| | pointer . Properties . IsRightButtonPressed & & ( PanWithMouseButtons & MouseButtons . RightButton ) ! = 0
& & ( PanWithMouseButtons & MouseButtons . RightButton ) ! = 0
)
)
| | ! AutoPan
| | ! AutoPan
| | SizeMode ! = SizeModes . Normal
| | SizeMode ! = SizeModes . Normal
@ -2669,12 +2620,8 @@ public class AdvancedImageBox : TemplatedControl
case SizeModes . Normal :
case SizeModes . Normal :
if ( AutoCenter )
if ( AutoCenter )
{
{
xOffset = (
xOffset = ( ! IsHorizontalBarVisible ? ( viewPortSize . Width - ScaledImageWidth ) / 2 : 0 ) ;
! IsHorizontalBarVisible ? ( viewPortSize . Width - ScaledImageWidth ) / 2 : 0
yOffset = ( ! IsVerticalBarVisible ? ( viewPortSize . Height - ScaledImageHeight ) / 2 : 0 ) ;
) ;
yOffset = (
! IsVerticalBarVisible ? ( viewPortSize . Height - ScaledImageHeight ) / 2 : 0
) ;
}
}
width = Math . Min ( ScaledImageWidth - Math . Abs ( Offset . X ) , viewPortSize . Width ) ;
width = Math . Min ( ScaledImageWidth - Math . Abs ( Offset . X ) , viewPortSize . Width ) ;
@ -2729,12 +2676,7 @@ public class AdvancedImageBox : TemplatedControl
var pixelSize = SelectionPixelSize ;
var pixelSize = SelectionPixelSize ;
using var frameBuffer = image . Lock ( ) ;
using var frameBuffer = image . Lock ( ) ;
var newBitmap = new WriteableBitmap (
var newBitmap = new WriteableBitmap ( pixelSize , image . Dpi , frameBuffer . Format , AlphaFormat . Unpremul ) ;
pixelSize ,
image . Dpi ,
frameBuffer . Format ,
AlphaFormat . Unpremul
) ;
using var newFrameBuffer = newBitmap . Lock ( ) ;
using var newFrameBuffer = newBitmap . Lock ( ) ;
var i = 0 ;
var i = 0 ;