forked from andrewkirillov/AForge.NET
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathPictureBox.cs
88 lines (81 loc) · 3.45 KB
/
PictureBox.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// AForge Controls Library
// AForge.NET framework
// http://www.aforgenet.com/framework/
//
// Copyright © AForge.NET, 2005-2011
// contacts@aforgenet.com
//
using System;
using System.Drawing;
using System.Drawing.Imaging;
namespace AForge.Controls
{
/// <summary>
/// Picture box control for displaying an image.
/// </summary>
///
/// <remarks><para>This control is inherited from System.Windows.Forms.PictureBox and is
/// aimed to resolve one of its issues - inability to display images with high color depth,
/// like 16 bpp grayscale, 48 bpp and 64 bpp color images. .NET framework does not handle
/// 16 bpp grayscale images at all, throwing exception when user tries to display them. Color
/// images with 48 bpp and 64 bpp are "kind of" supported, but only maximum of 13 bits for each
/// color plane are allowed. Therefore this control is created, which allows to display as
/// 16 bpp grayscale images, as 48 bpp and 64 bpp color images.</para>
///
/// <para><note>To display high color depth images, the control does internal conversion of them
/// to lower color depth images - 8 bpp grayscale, 24 bpp and 32 bpp color images respectively. In
/// the case source image already has low color depth, it is displayed without any conversions.
/// </note></para>
/// </remarks>
///
public class PictureBox : System.Windows.Forms.PictureBox
{
private Image sourceImage = null;
private Image convertedImage = null;
/// <summary>
/// Gets or sets the image that the PictureBox displays.
/// </summary>
///
/// <remarks><para>The property is used to set image to be displayed or to get currently
/// displayed image.</para>
///
/// <para><note>In the case if source image has high color depth, like 16 bpp grayscale image,
/// 48 bpp or 64 bpp color image, it is converted to lower color depth before displaying -
/// to 8 bpp grayscale, 24 bpp or 32 bpp color image respectively.</note></para>
///
/// <para><note>During color conversion the original source image is kept unmodified, but internal
/// converted copy is created. The property always returns original source image.</note></para>
/// </remarks>
///
public new Image Image
{
get { return sourceImage; }
set
{
// check source image format
if (
( value != null ) && ( value is Bitmap ) &&
( ( value.PixelFormat == PixelFormat.Format16bppGrayScale ) ||
( value.PixelFormat == PixelFormat.Format48bppRgb) ||
( value.PixelFormat == PixelFormat.Format64bppArgb ) ) )
{
// convert and display image
Image tempImage = AForge.Imaging.Image.Convert16bppTo8bpp( (Bitmap) value );
base.Image = tempImage;
// dispose previous image if required
if ( convertedImage != null )
{
convertedImage.Dispose( );
}
convertedImage = tempImage;
}
else
{
// display source image as it is
base.Image = value;
}
sourceImage = value;
}
}
}
}