WinForm.pas 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. {
  2. Vampyre Imaging Library Demo
  3. Demo01 (Delphi.NET, dll library usage, dotNET)
  4. tested in BDS 2006
  5. written by Marek Mauder
  6. Simple image viewer program. It loads image stored in one of file formats
  7. supported by Imaging library and assigns it to PictureBox component.
  8. You can get some information about loaded image by clicking on PictureBox.
  9. }
  10. unit WinForm;
  11. interface
  12. uses
  13. System.Drawing, System.Drawing.Imaging, System.Collections, System.ComponentModel,
  14. System.Windows.Forms, System.Data, System.Diagnostics, System.IO, System.Resources,
  15. // Imaging dotNET wrapper
  16. ImagingNET;
  17. type
  18. TWinForm = class(System.Windows.Forms.Form)
  19. {$REGION 'Designer Managed Code'}
  20. strict private
  21. /// <summary>
  22. /// Required designer variable.
  23. /// </summary>
  24. components: System.ComponentModel.IContainer;
  25. Button: System.Windows.Forms.Button;
  26. Picture: System.Windows.Forms.PictureBox;
  27. OpenFile: System.Windows.Forms.OpenFileDialog;
  28. Panel1: System.Windows.Forms.Panel;
  29. Link: System.Windows.Forms.LinkLabel;
  30. /// <summary>
  31. /// Required method for Designer support - do not modify
  32. /// the contents of this method with the code editor.
  33. /// </summary>
  34. procedure InitializeComponent;
  35. procedure Button_Click(sender: System.Object; e: System.EventArgs);
  36. procedure Picture_Click(sender: System.Object; e: System.EventArgs);
  37. procedure Link_LinkClicked(sender: System.Object; e: System.Windows.Forms.LinkLabelLinkClickedEventArgs);
  38. {$ENDREGION}
  39. strict protected
  40. procedure Dispose(Disposing: Boolean); override;
  41. private
  42. Info: TImageFormatInfo;
  43. ImgFile: string;
  44. ImgWidth: Int32;
  45. ImgHeight: Int32;
  46. Img: TImageData;
  47. public
  48. constructor Create;
  49. end;
  50. [assembly: RuntimeRequiredAttribute(TypeOf(TWinForm))]
  51. implementation
  52. {$AUTOBOX ON}
  53. {$REGION 'Windows Form Designer generated code'}
  54. /// <summary>
  55. /// Required method for Designer support -- do not modify
  56. /// the contents of this method with the code editor.
  57. /// </summary>
  58. procedure TWinForm.InitializeComponent;
  59. var
  60. resources: System.Resources.ResourceManager;
  61. begin
  62. resources := System.Resources.ResourceManager.Create(TypeOf(TWinForm));
  63. Self.Picture := System.Windows.Forms.PictureBox.Create;
  64. Self.OpenFile := System.Windows.Forms.OpenFileDialog.Create;
  65. Self.Panel1 := System.Windows.Forms.Panel.Create;
  66. Self.Link := System.Windows.Forms.LinkLabel.Create;
  67. Self.Button := System.Windows.Forms.Button.Create;
  68. Self.Panel1.SuspendLayout;
  69. Self.SuspendLayout;
  70. //
  71. // Picture
  72. //
  73. Self.Picture.Anchor := (System.Windows.Forms.AnchorStyles((((System.Windows.Forms.AnchorStyles.Top
  74. or System.Windows.Forms.AnchorStyles.Bottom) or System.Windows.Forms.AnchorStyles.Left)
  75. or System.Windows.Forms.AnchorStyles.Right)));
  76. Self.Picture.BackColor := System.Drawing.Color.FromArgb((Byte(192)), (Byte(255)),
  77. (Byte(255)));
  78. Self.Picture.BorderStyle := System.Windows.Forms.BorderStyle.FixedSingle;
  79. Self.Picture.Cursor := System.Windows.Forms.Cursors.Hand;
  80. Self.Picture.Location := System.Drawing.Point.Create(0, 35);
  81. Self.Picture.Name := 'Picture';
  82. Self.Picture.Size := System.Drawing.Size.Create(500, 265);
  83. Self.Picture.SizeMode := System.Windows.Forms.PictureBoxSizeMode.CenterImage;
  84. Self.Picture.TabIndex := 1;
  85. Self.Picture.TabStop := False;
  86. Include(Self.Picture.Click, Self.Picture_Click);
  87. //
  88. // OpenFile
  89. //
  90. Self.OpenFile.Filter := 'Imaging Files (*.jpg;*.png;*.dds;*.mng;*.jng;*.bm' +
  91. 'p;*.tga)|*.jpg;*.png;*.dds;*.mng;*.jng;*.bmp;*.tga|All files (*.*)|*.*';
  92. //
  93. // Panel1
  94. //
  95. Self.Panel1.BorderStyle := System.Windows.Forms.BorderStyle.FixedSingle;
  96. Self.Panel1.Controls.Add(Self.Link);
  97. Self.Panel1.Controls.Add(Self.Button);
  98. Self.Panel1.Dock := System.Windows.Forms.DockStyle.Top;
  99. Self.Panel1.Location := System.Drawing.Point.Create(0, 0);
  100. Self.Panel1.Name := 'Panel1';
  101. Self.Panel1.Size := System.Drawing.Size.Create(492, 35);
  102. Self.Panel1.TabIndex := 2;
  103. //
  104. // Link
  105. //
  106. Self.Link.Anchor := (System.Windows.Forms.AnchorStyles((System.Windows.Forms.AnchorStyles.Top
  107. or System.Windows.Forms.AnchorStyles.Right)));
  108. Self.Link.LinkArea := System.Windows.Forms.LinkArea.Create(0, 522);
  109. Self.Link.Location := System.Drawing.Point.Create(305, 5);
  110. Self.Link.Name := 'Link';
  111. Self.Link.Size := System.Drawing.Size.Create(180, 25);
  112. Self.Link.TabIndex := 2;
  113. Self.Link.TabStop := True;
  114. Self.Link.Text := 'http://imaginglib.sourceforge.net';
  115. Self.Link.TextAlign := System.Drawing.ContentAlignment.MiddleCenter;
  116. Include(Self.Link.LinkClicked, Self.Link_LinkClicked);
  117. //
  118. // Button
  119. //
  120. Self.Button.Location := System.Drawing.Point.Create(5, 5);
  121. Self.Button.Name := 'Button';
  122. Self.Button.Size := System.Drawing.Size.Create(75, 25);
  123. Self.Button.TabIndex := 1;
  124. Self.Button.Text := 'Load Image';
  125. Include(Self.Button.Click, Self.Button_Click);
  126. //
  127. // TWinForm
  128. //
  129. Self.AutoScaleBaseSize := System.Drawing.Size.Create(5, 13);
  130. Self.ClientSize := System.Drawing.Size.Create(492, 296);
  131. Self.Controls.Add(Self.Panel1);
  132. Self.Controls.Add(Self.Picture);
  133. Self.Icon := (System.Drawing.Icon(resources.GetObject('$this.Icon')));
  134. Self.Name := 'TWinForm';
  135. Self.StartPosition := System.Windows.Forms.FormStartPosition.CenterScreen;
  136. Self.Text := 'Imaging.NET Demo 01';
  137. Self.Panel1.ResumeLayout(False);
  138. Self.ResumeLayout(False);
  139. end;
  140. {$ENDREGION}
  141. procedure TWinForm.Dispose(Disposing: Boolean);
  142. begin
  143. if Disposing then
  144. begin
  145. if Components <> nil then
  146. Components.Dispose();
  147. end;
  148. // Free source image
  149. Imaging.FreeImage(Img);
  150. inherited Dispose(Disposing);
  151. end;
  152. constructor TWinForm.Create;
  153. var
  154. Major, Minor, Patch: Int32;
  155. begin
  156. inherited Create;
  157. InitializeComponent;
  158. // Get imaging version
  159. Imaging.GetVersion(Major, Minor, Patch);
  160. Self.Text := Self.Text + System.&String.Format(' (Imaging library version {0}.{1}.{2})',
  161. [Major, Minor, Patch]);
  162. end;
  163. procedure TWinForm.Link_LinkClicked(sender: System.Object; e: System.Windows.Forms.LinkLabelLinkClickedEventArgs);
  164. begin
  165. System.Diagnostics.Process.Start(Link.Text);
  166. end;
  167. procedure TWinForm.Picture_Click(sender: System.Object; e: System.EventArgs);
  168. const
  169. NewLine = #13#10;
  170. var
  171. Msg: System.&String;
  172. begin
  173. if System.IO.File.Exists(ImgFile) then
  174. Msg := 'File Name: ' + ImgFile + NewLine +
  175. 'Width: ' + Img.Width.ToString + NewLine +
  176. 'Height: ' + Img.Height.ToString + NewLine+
  177. 'Format: ' + Info.Name + NewLine +
  178. 'Size: ' + Single(Imaging.GetPixelsSize(Info.Format, Img.Width, Img.Height) / 1024).ToString +
  179. ' KiB ' + NewLine
  180. else
  181. Msg := 'No image loaded';
  182. MessageBox.Show(Msg, 'Image Info',
  183. MessageBoxButtons.OK, MessageBoxIcon.Information);
  184. end;
  185. procedure TWinForm.Button_Click(sender: System.Object; e: System.EventArgs);
  186. var
  187. Stride: LongInt;
  188. begin
  189. if (OpenFile.ShowDialog = System.Windows.Forms.DialogResult.OK) then
  190. begin
  191. // Free old image
  192. Imaging.FreeImage(Img);
  193. // Load and test image
  194. if Imaging.LoadImageFromFile(OpenFile.FileName, Img) and Imaging.TestImage(Img) then
  195. begin
  196. // Get original image frmat info
  197. Imaging.GetImageFormatInfo(Img.Format, Info);
  198. ImgWidth := Img.Width;
  199. ImgHeight := Img.Height;
  200. ImgFile := OpenFile.FileName;
  201. // Convert image to 32bit ARGB format
  202. Imaging.ConvertImage(Img, ifA8R8G8B8);
  203. // Get number of bytes per one line
  204. Stride := Imaging.GetPixelsSize(Img.Format, Img.Width, 1);
  205. // Create dotNET bitmap and assign it to PictureBox
  206. if Picture.Image <> nil then
  207. Picture.Image.Dispose;
  208. Picture.Image := Bitmap.Create(Img.Width, Img.Height, Stride,
  209. PixelFormat.Format32bppArgb, Img.Bits);
  210. // We cannot free source image even though we will not
  211. // use it any more. It looks like Bitmap uses Img.Bits directly
  212. // without making its own copy
  213. end
  214. else
  215. begin
  216. MessageBox.Show('Error when loading selected image', 'Imaging Error',
  217. MessageBoxButtons.OK, MessageBoxIcon.Error);
  218. end;
  219. end;
  220. end;
  221. end.