PageRenderTime 66ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/NuGenImageWorks/x86/PhotoMenu.cs

https://github.com/AnthonyNystrom/GenXSource
C# | 293 lines | 218 code | 52 blank | 23 comment | 20 complexity | 25c719412c2590cf3b29ec42789d2e4b MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Drawing.Imaging;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.IO;
  9. using System.Xml;
  10. using Genetibase.UI.NuGenImageWorks.WaitCursor;
  11. using Genetibase.UI.FileSaveDialogEx;
  12. namespace Genetibase.UI.NuGenImageWorks
  13. {
  14. public partial class PhotoMenu : RibbonPopup
  15. {
  16. private bool cancelclose;
  17. private Form parent = null;
  18. public PhotoMenu(Form parent)
  19. {
  20. InitializeComponent();
  21. this.parent = parent;
  22. }
  23. private void PhotoMenu_Paint(object sender, PaintEventArgs e)
  24. {
  25. e.Graphics.DrawRectangle(new Pen(Color.FromArgb(115, 115, 115)), 0, 0, this.Width - 1, this.Height - 1);
  26. }
  27. //Document _psd = null;
  28. private void ribbonButton1_Click(object sender, EventArgs e)
  29. {
  30. try
  31. {
  32. this.cancelclose = true;
  33. if (this.openFileDialog1.ShowDialog(this.parent) == DialogResult.OK)
  34. {
  35. OpenFile(this.openFileDialog1.FileName);
  36. }
  37. this.Close();
  38. }
  39. catch { }
  40. }
  41. private void OpenFile(string FileName)
  42. {
  43. this.Hide();
  44. Application.DoEvents();
  45. //((MainForm)this.parent).Clear();
  46. String ext = Path.GetExtension(FileName).ToLower();
  47. MainForm frmMain = ((MainForm)this.parent);
  48. try
  49. {
  50. Directory.Delete(frmMain.TempDirectory, true);
  51. }
  52. catch { }
  53. //if (ext == ".psd")
  54. //{
  55. // try
  56. // {
  57. // PSD.PSDDocument psdDoc = new PSD.PSDDocument();
  58. // psdDoc.LoadFile(this.openFileDialog1.FileName);
  59. // frmMain.imageViewer1.SourceDirectory = psdDoc.TempDirectory;
  60. // frmMain.TempDirectory = psdDoc.TempDirectory;
  61. // }
  62. // catch { }
  63. // finally
  64. // {
  65. // this.Close();
  66. // }
  67. // return;
  68. //}
  69. string fileSize = Utility.GetFileSize(FileName);
  70. FileStream fs = File.Open(FileName, FileMode.Open, FileAccess.Read);
  71. Program.Photo = new Bitmap(fs);
  72. fs.Close();
  73. string[] fn = FileName.Split('\\');
  74. Program.Title.Text = "File: " + fn[fn.Length - 1] + " Resolution: " + Program.Photo.Width.ToString() + " x " + Program.Photo.Height.ToString() + " Size: " + fileSize;
  75. Program.FileName = FileName;
  76. ((MainForm)this.parent).Open();
  77. Program.Source.Size = Program.Destination.Size;
  78. Program.Optimize2();
  79. //Program.Optimize(Program.Source.Width, Program.Source.Height);
  80. Program.Destination.Image = (Bitmap)Program.Source.Image.Clone();
  81. }
  82. private void ribbonButton2_Click(object sender, EventArgs e)
  83. {
  84. //Save AS
  85. MainForm mainForm = (MainForm)this.parent;
  86. Cursor oldCursor = mainForm.Cursor;
  87. mainForm.Cursor = ApplicationWaitCursor.Cursor;
  88. string fileName = "";
  89. DialogResult res = DialogResult.None;
  90. int filterIndex = 0;
  91. try
  92. {
  93. this.cancelclose = true;
  94. if (Effects2.BoxFilter.Angle > 0)
  95. {
  96. FormSaveFileDialog saveDlg = new FormSaveFileDialog(mainForm);
  97. saveDlg.SaveDialog.Filter = this.saveFileDialog1.Filter;
  98. res = saveDlg.ShowDialog();
  99. fileName = saveDlg.SaveDialog.FileName;
  100. filterIndex = saveDlg.SaveDialog.FilterIndex;
  101. }
  102. else
  103. {
  104. res = this.saveFileDialog1.ShowDialog();
  105. fileName = this.saveFileDialog1.FileName;
  106. filterIndex = this.saveFileDialog1.FilterIndex;
  107. }
  108. if (res != DialogResult.OK)
  109. {
  110. Extra.BackgroundColor = Color.Empty;
  111. Extra.Enable = false;
  112. }
  113. if ( res == DialogResult.OK)
  114. {
  115. this.Hide();
  116. Application.DoEvents();
  117. MainForm frmMain = ((MainForm)this.parent);
  118. Bitmap photo = frmMain.FilterEffectsForSave();
  119. switch (filterIndex)
  120. {
  121. case 1:
  122. photo.Save(fileName, ImageFormat.Bmp);
  123. break;
  124. case 2:
  125. ImageCodecInfo codecinfo = null;
  126. ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders();
  127. for (int i = 0; i < encoders.Length; i++)
  128. {
  129. if (encoders[i].MimeType == "image/jpeg")
  130. codecinfo = encoders[i];
  131. }
  132. EncoderParameters ep = new EncoderParameters(1);
  133. ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)100);
  134. photo.Save(fileName, codecinfo, ep);
  135. break;
  136. case 3:
  137. photo.Save(fileName, ImageFormat.Gif);
  138. break;
  139. case 4:
  140. photo.Save(fileName, ImageFormat.Tiff);
  141. break;
  142. case 5:
  143. photo.Save(fileName, ImageFormat.Png);
  144. break;
  145. default:
  146. photo.Save(fileName);
  147. break;
  148. }
  149. photo.Dispose();
  150. }
  151. Extra.Enable = false;
  152. Extra.BackgroundColor = Color.Empty;
  153. this.Close();
  154. }
  155. catch { }
  156. finally
  157. {
  158. mainForm.Cursor = oldCursor;
  159. }
  160. }
  161. private void Backup(string FileName)
  162. {
  163. try
  164. {
  165. if(File.Exists(FileName + ".iwbak") )
  166. return;
  167. File.Copy(FileName,FileName + ".iwbak");
  168. }
  169. catch { }
  170. }
  171. private void ribbonButton4_Click(object sender, EventArgs e)
  172. {
  173. // Save
  174. MainForm mainForm = (MainForm)this.parent;
  175. Cursor oldCursor = mainForm.Cursor;
  176. try
  177. {
  178. mainForm.Cursor = ApplicationWaitCursor.Cursor;
  179. this.Hide();
  180. Application.DoEvents();
  181. if (Program.Destination != null && Program.Destination.Image != null)
  182. {
  183. MainForm frmMain = ((MainForm)this.parent);
  184. if (Effects2.BoxFilter.Angle > 0)
  185. {
  186. frmSelectBackGround frmSelectBG = new frmSelectBackGround(mainForm);
  187. DialogResult res = frmSelectBG.ShowDialog();
  188. if (res == DialogResult.Cancel)
  189. {
  190. Extra.Enable = false;
  191. Extra.BackgroundColor = Color.Empty;
  192. return;
  193. }
  194. }
  195. Bitmap photo = frmMain.FilterEffectsForSave();
  196. //Program.Destination.Image.Save(Program.FileName, Program.Source.Image.RawFormat);
  197. ImageFormat format = Program.Photo.RawFormat;
  198. //Create a backup if one already doesn't exist
  199. this.Backup(Program.FileName);
  200. photo.Save(Program.FileName, format);
  201. mainForm.imageViewer1.UpdateThumbnail(Program.FileName);
  202. Image old = Program.Source.Image;
  203. Program.Source.Image = (Image)Program.Destination.Image.Clone();
  204. Program.Source.Refresh();
  205. OpenFile(Program.FileName);
  206. Extra.Enable = false;
  207. Extra.BackgroundColor = Color.Empty;
  208. this.Close();
  209. }
  210. }
  211. catch { }
  212. finally
  213. {
  214. mainForm.Cursor = oldCursor;
  215. Extra.Enable = false;
  216. Extra.BackgroundColor = Color.Empty;
  217. }
  218. }
  219. private void ribbonButton3_Click(object sender, EventArgs e)
  220. {
  221. Application.Exit();
  222. }
  223. private void PhotoMenu_Deactivate(object sender, EventArgs e)
  224. {
  225. if (!this.cancelclose)
  226. this.Close();
  227. }
  228. private void btnHelp_Click(object sender, EventArgs e)
  229. {
  230. this.Hide();
  231. System.Diagnostics.Process.Start("http://www.genetibase.com/nugenimageworks.php");
  232. this.Close();
  233. }
  234. private void ribbonButton6_Click(object sender, EventArgs e)
  235. {
  236. this.Hide();
  237. Splash.ShowSplash(true);
  238. this.Close();
  239. }
  240. }
  241. }