summaryrefslogtreecommitdiff
path: root/Wrapper/FreeImage.NET/cs/Samples/Sample 04 - Getting bitmap informations/MainForm.cs
blob: cf72567b7030f99a69722e75cd6d40d0f63a9f69 (plain)
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using System;
using System.Windows.Forms;
using FreeImageAPI;

namespace Sample04
{
	public partial class MainForm : Form
	{
		string message = null;

		[STAThread]
		static void Main()
		{
			Application.EnableVisualStyles();
			Application.SetCompatibleTextRenderingDefault(false);
			Application.Run(new MainForm());
		}

		public MainForm()
		{
			InitializeComponent();
			FreeImageEngine.Message += new OutputMessageFunction(FreeImage_Message);
		}

		~MainForm()
		{
			FreeImageEngine.Message -= new OutputMessageFunction(FreeImage_Message);
		}

		void FreeImage_Message(FREE_IMAGE_FORMAT fif, string message)
		{
			if (this.message == null)
			{
				this.message = message;
			}
			else
			{
				this.message += "\n" + message;
			}
		}

		private void bOpenFile_Click(object sender, EventArgs e)
		{
			// Resetting filename
			ofd.FileName = "";

			// Was a file selected
			if (ofd.ShowDialog() == DialogResult.OK)
			{
				// Format is stored in 'format' on successfull load.
				FREE_IMAGE_FORMAT format = FREE_IMAGE_FORMAT.FIF_UNKNOWN;

				// Try loading the file
				FIBITMAP dib = FreeImage.LoadEx(ofd.FileName, ref format);

				try
				{
					// Error handling
					if (dib.IsNull)
					{
						// Chech whether FreeImage generated an error messe
						if (message != null)
						{
							MessageBox.Show("File could not be loaded!\nError:{0}", message);
						}
						else
						{
							MessageBox.Show("File could not be loaded!", message);
						}
						return;
					}
					
					// Read width
					lWidth.Text = String.Format("Width: {0}", FreeImage.GetWidth(dib));
					
					// Read height
					lHeight.Text = String.Format("Width: {0}", FreeImage.GetWidth(dib));
					
					// Read color depth
					lBPP.Text = String.Format("Color Depth: {0}", FreeImage.GetBPP(dib));
					
					// Read red bitmask (16 - 32 bpp)
					lRedMask.Text = String.Format("Red Mask: 0x{0:X8}", FreeImage.GetRedMask(dib));

					// Read green bitmask (16 - 32 bpp)
					lBlueMask.Text = String.Format("Green Mask: 0x{0:X8}", FreeImage.GetGreenMask(dib));

					// Read blue bitmask (16 - 32 bpp)
					lGreenMask.Text = String.Format("Blue Mask: 0x{0:X8}", FreeImage.GetBlueMask(dib));

					// Read image type (FI_BITMAP, FIT_RGB16, FIT_COMPLEX ect)
					lImageType.Text = String.Format("Image Type: {0}", FreeImage.GetImageType(dib));
					
					// Read x-axis dpi
					lDPIX.Text = String.Format("DPI X: {0}", FreeImage.GetResolutionX(dib));
					
					// Read y-axis dpi
					lDPIY.Text = String.Format("DPI Y: {0}", FreeImage.GetResolutionY(dib));
					
					// Read file format
					lFormat.Text = String.Format("File Format: {0}", FreeImage.GetFormatFromFIF(format));
				}
				catch
				{
				}
				
				// Always unload bitmap
				FreeImage.UnloadEx(ref dib);
				
				// Reset the error massage buffer
				message = null;
			}
			// No file was selected
			else
			{
				MessageBox.Show("No file loaded.", "Error");
			}
		}
	}
}