PK e;' ' VSImageVisualizer/GUI/ImageDisplay.xaml
PK +e;Υ * VSImageVisualizer/GUI/ImageDisplay.xaml.csusing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ImageVisualizer.Zoom;
namespace ImageVisualizer.GUI
{
///
/// Interaction logic for ImageDisplay.xaml
///
public partial class ImageDisplay : UserControl
{
#region Fields
System.Drawing.Bitmap _sourceBmp;
#endregion
#region Constructors
public ImageDisplay(System.Drawing.Bitmap sourceBitmap)
: this()
{
_sourceBmp=sourceBitmap;
DisplayImage.Source = ImageConverters.ConvertBitmapToBitmapSource(sourceBitmap);
}
private ImageDisplay()
{
InitializeComponent();
}
#endregion
#region Properties
internal double Zoom
{
get { return (double)GetValue(ZoomProperty); }
set { SetValue(ZoomProperty, value); }
}
#endregion
#region Methods
bool IsCtrlPressed()
{
bool IsLeftCtrlDown = (Keyboard.GetKeyStates(Key.LeftCtrl) == KeyStates.Down);
bool IsRightCtrlDown = (Keyboard.GetKeyStates(Key.RightCtrl) == KeyStates.Down);
return IsLeftCtrlDown || IsRightCtrlDown;
}
void ChangeZoomLevel(bool increaseZoom)
{
double newZoomFactor = Zoom + (increaseZoom ? 0.1 : -0.1);
try
{
Zoom = newZoomFactor;
}
catch (ArgumentException)
{
//Value was out of range. Just don't zoom
}
}
private void UpdateInfoLabel()
{
Point mousePositionInImage = Mouse.GetPosition(DisplayImage);
int mouseX = (int)mousePositionInImage.X;
int mouseY = (int)mousePositionInImage.Y;
System.Drawing.Color pixelColor = _sourceBmp.GetPixel(mouseX, mouseY);
string hexColor = System.Drawing.ColorTranslator.ToHtml(pixelColor);
UpdateInfoLabel((int)mousePositionInImage.X,
(int)mousePositionInImage.Y,
hexColor);
}
private void UpdateInfoLabel(int mouseX, int mouseY, string colorValue)
{
const string PixelInfoText = "({0}x{1}) {2}";
PixelInfo.Content = string.Format(PixelInfoText, mouseX, mouseY, colorValue);
}
#endregion
#region Event Handlers
private static bool ZoomChanged(object value)
{
return ZoomConverter.IsZoomValueValid((double)value);
}
private void DisplayImage_MouseWheel(object sender, MouseWheelEventArgs e)
{
if (IsCtrlPressed())
{
bool increaseZoom = (e.Delta>=0);
ChangeZoomLevel(increaseZoom);
e.Handled = true;
}
}
private void DisplayImage_MouseMove(object sender, MouseEventArgs e)
{
UpdateInfoLabel();
}
#endregion
#region Dependency Properties
///
/// Dependency property for image zoom
///
/// Pay attention that the default value is 1.0 (double) and not 1 (int)
private static readonly DependencyProperty ZoomProperty = DependencyProperty.Register("Zoom", typeof(double), typeof(ImageDisplay),
new FrameworkPropertyMetadata(1.0, FrameworkPropertyMetadataOptions.AffectsMeasure), new ValidateValueCallback(ZoomChanged));
#endregion
}
}
PK !p;q3
3
$ VSImageVisualizer/GUI/ImageWindow.cs//Copyright (c) Victor Hurdugaci
//http://victorhurdugaci.com
//This code is licensed under the Non-Profit Open Software License version 3.0
//Full license text: http://www.opensource.org/licenses/NOSL3.0.html
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Runtime.InteropServices;
using System.Windows.Input;
using System.Windows.Controls.Primitives;
namespace ImageVisualizer.GUI
{
///
/// The window that holds the image display control and which is also the debugger window
///
class ImageWindow : Window
{
#region Constants
private readonly double DefaultMinimumWidth = 200;
private readonly double DefaultMinimumHeight = 200;
private readonly double DefaultWindowWidth = 640;
private readonly double DefaultWindowHeight = 480;
private readonly string WindowTitle = "Debug Image Visualizer";
#endregion
#region Fields
ImageDisplay _imageDisplay;
#endregion
#region Constructors
public ImageWindow(System.Drawing.Image sourceImage)
: this(new System.Drawing.Bitmap(sourceImage))
{ }
public ImageWindow(System.Drawing.Bitmap sourceBitmap)
: base()
{
_imageDisplay = new ImageDisplay(sourceBitmap);
InitInstance();
}
#endregion
#region Methods
void InitInstance()
{
#region Window Properties
this.MinWidth = DefaultMinimumWidth;
this.MinHeight = DefaultMinimumHeight;
this.Height = DefaultWindowHeight;
this.Width = DefaultWindowWidth;
this.ShowInTaskbar = false;
this.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterOwner;
this.Title = WindowTitle;
#endregion
#region Event Handlers
this.Loaded += new RoutedEventHandler(ImageWindow_Loaded);
#endregion
#region Window controls
this.AddChild(_imageDisplay);
#endregion
}
#endregion
#region Event Handlers
void ImageWindow_Loaded(object sender, RoutedEventArgs e)
{
WindowHelpers.DisableMinimizeBox(this);
}
#endregion
}
}
PK !p;< < &