Skip to content

Commit bbf5aaa

Browse files
committed
Move image parsing from ImageControl to ImageForm
1 parent 1308805 commit bbf5aaa

File tree

3 files changed

+62
-56
lines changed

3 files changed

+62
-56
lines changed

src/ImageVisualizer14/ImageControl.xaml.cs

Lines changed: 3 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -17,66 +17,18 @@ public partial class ImageControl : UserControl
1717
Point? lastMousePositionOnTarget;
1818
Point? lastDragPoint;
1919

20-
[DllImport("gdi32")]
21-
[return: MarshalAs(UnmanagedType.Bool)]
22-
static extern bool DeleteObject(IntPtr hObject);
23-
2420
double _zoomValue = 1.0;
2521

2622
public ImageControl()
2723
{
2824
InitializeComponent();
2925
}
3026

31-
public void SetImage(object sourceBitmap)
27+
public void SetImage(ImageSource imageSource)
3228
{
33-
if (sourceBitmap != null)
29+
if (imageSource != null)
3430
{
35-
#if DEBUG
36-
string expression = sourceBitmap.ToString();
37-
#endif
38-
39-
var method = sourceBitmap.GetType().GetMethod("ToBitmap", new Type[] { });
40-
if (method != null)
41-
{
42-
sourceBitmap = method.Invoke(sourceBitmap, null);
43-
}
44-
45-
if (sourceBitmap is System.Drawing.Bitmap)
46-
{
47-
BitmapSource bitmap = null;
48-
49-
var hObject = ((System.Drawing.Bitmap)sourceBitmap).GetHbitmap();
50-
51-
try
52-
{
53-
bitmap = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
54-
hObject,
55-
IntPtr.Zero,
56-
Int32Rect.Empty,
57-
BitmapSizeOptions.FromEmptyOptions());
58-
}
59-
catch(Win32Exception)
60-
{
61-
bitmap = null;
62-
}
63-
finally
64-
{
65-
DeleteObject(hObject);
66-
}
67-
68-
if(bitmap != null)
69-
{
70-
DisplayImage.Width = bitmap.Width;
71-
DisplayImage.Height = bitmap.Height;
72-
73-
DisplayImage.Source = bitmap;
74-
}
75-
}
76-
else if (sourceBitmap is SerializableBitmapImage source)
77-
{
78-
DisplayImage.Source = source;
79-
}
31+
DisplayImage.Source = imageSource;
8032
}
8133
}
8234

src/ImageVisualizer14/ImageForm.cs

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
using System;
2+
using System.ComponentModel;
23
using System.Drawing;
4+
using System.Runtime.InteropServices;
5+
using System.Windows;
36
using System.Windows.Forms;
7+
using System.Windows.Media.Imaging;
48
using Microsoft.VisualStudio.DebuggerVisualizers;
59

610
namespace Aberus.VisualStudio.Debugger.ImageVisualizer
711
{
812
public partial class ImageForm : Form
913
{
14+
[DllImport("gdi32")]
15+
[return: MarshalAs(UnmanagedType.Bool)]
16+
static extern bool DeleteObject(IntPtr hObject);
17+
1018
public static Font UIFont
1119
{
1220
get
@@ -26,7 +34,7 @@ public static Font UIFont
2634
#elif VS16
2735
var dteProgID = "VisualStudio.DTE.16.0";
2836
#endif
29-
var dte = (EnvDTE.DTE)System.Runtime.InteropServices.Marshal.GetActiveObject(dteProgID);
37+
var dte = (EnvDTE.DTE)Marshal.GetActiveObject(dteProgID);
3038
var fontProperty = dte.Properties["FontsAndColors", "Dialogs and Tool Windows"];
3139
if (fontProperty != null)
3240
{
@@ -39,7 +47,7 @@ public static Font UIFont
3947
return font;
4048
}
4149

42-
return SystemFonts.DefaultFont;
50+
return System.Drawing.SystemFonts.DefaultFont;
4351
}
4452
}
4553

@@ -57,7 +65,53 @@ public ImageForm(IVisualizerObjectProvider objectProvider)
5765
this.txtExpression.Font = UIFont;
5866
this.btnClose.Font = UIFont;
5967

60-
imageControl.SetImage(objectProvider.GetObject());
68+
object objectBitmap = objectProvider.GetObject();
69+
if (objectBitmap != null)
70+
{
71+
#if DEBUG
72+
string expression = objectBitmap.ToString();
73+
#endif
74+
75+
var method = objectBitmap.GetType().GetMethod("ToBitmap", new Type[] { });
76+
if (method != null)
77+
{
78+
objectBitmap = method.Invoke(objectBitmap, null);
79+
}
80+
81+
BitmapSource bitmapSource = null;
82+
83+
if (objectBitmap is Bitmap)
84+
{
85+
var hObject = ((Bitmap)objectBitmap).GetHbitmap();
86+
87+
try
88+
{
89+
bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
90+
hObject,
91+
IntPtr.Zero,
92+
Int32Rect.Empty,
93+
BitmapSizeOptions.FromEmptyOptions());
94+
}
95+
catch (Win32Exception)
96+
{
97+
bitmapSource = null;
98+
}
99+
finally
100+
{
101+
DeleteObject(hObject);
102+
}
103+
}
104+
else if (objectBitmap is SerializableBitmapImage serializableBitmapImage)
105+
{
106+
bitmapSource = serializableBitmapImage;
107+
}
108+
109+
if (bitmapSource != null)
110+
{
111+
imageControl.SetImage(bitmapSource);
112+
}
113+
}
114+
61115
txtExpression.Text = objectProvider.GetObject().ToString();
62116
}
63117

src/ImageVisualizerPackage/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,5 @@
2929
// You can specify all the values or you can default the Build and Revision Numbers
3030
// by using the '*' as shown below:
3131
// [assembly: AssemblyVersion("1.0.*")]
32-
[assembly: AssemblyVersion("0.7.0.0")]
33-
[assembly: AssemblyFileVersion("0.7.0.0")]
32+
[assembly: AssemblyVersion("0.8.0.0")]
33+
[assembly: AssemblyFileVersion("0.8.0.0")]

0 commit comments

Comments
 (0)