private void UpdateDataView() { if (m_bInitializing) { return; } string strViewer = m_tscViewers.Text; bool bText = ((strViewer == m_strViewerText) || (strViewer == m_strViewerWeb)); bool bImage = (strViewer == m_strViewerImage); UpdateVisibility(strViewer, false); m_tssSeparator0.Visible = (bText || bImage); m_tslEncoding.Visible = m_tscEncoding.Visible = bText; m_tslZoom.Visible = m_tscZoom.Visible = m_tsbZoomOut.Visible = m_tsbZoomIn.Visible = bImage; try { if (this.DvfUpdating != null) { DvfContextEventArgs args = new DvfContextEventArgs(this, m_pbData, m_strDataDesc, m_tscViewers); this.DvfUpdating(this, args); if (args.Cancel) { return; } } if (strViewer == m_strViewerHex) { UpdateHexView(); } else if (strViewer == m_strViewerText) { string strData = BinaryDataToString(true); SetRtbData(strData, (m_bdc == BinaryDataClass.RichText), false); } else if (strViewer == m_strViewerImage) { if (m_img == null) { m_img = GfxUtil.LoadImage(m_pbData); } UpdateImageView(); } else if (strViewer == m_strViewerWeb) { string strData = BinaryDataToString(false); UIUtil.SetWebBrowserDocument(m_webBrowser, strData); } } catch (Exception) { } UpdateVisibility(strViewer, true); }
public void TestLoadImage() { var testData = Convert.FromBase64String(TestImageData); //var image = GfxUtil.ScaleImage(testData, 16, 16, ScaleTransformFlags.UIIcon); var image = GfxUtil.LoadImage(testData); Assert.That(image.Width, Is.EqualTo(16)); Assert.That(image.Height, Is.EqualTo(16)); }
private static void ImportIcon(XmlNode xn, PwEntry pe, PwDatabase pd) { XmlNode xnIcon = xn.Attributes.GetNamedItem("ICON"); if(xnIcon == null) return; string strIcon = xnIcon.Value; if(!StrUtil.IsDataUri(strIcon)) { Debug.Assert(false); return; } try { byte[] pbImage = StrUtil.DataUriToData(strIcon); if((pbImage == null) || (pbImage.Length == 0)) { Debug.Assert(false); return; } Image img = GfxUtil.LoadImage(pbImage); if(img == null) { Debug.Assert(false); return; } byte[] pbPng; int wMax = PwCustomIcon.MaxWidth; int hMax = PwCustomIcon.MaxHeight; if((img.Width <= wMax) && (img.Height <= hMax)) { using(MemoryStream msPng = new MemoryStream()) { img.Save(msPng, ImageFormat.Png); pbPng = msPng.ToArray(); } } else { using(Image imgSc = GfxUtil.ScaleImage(img, wMax, hMax)) { using(MemoryStream msPng = new MemoryStream()) { imgSc.Save(msPng, ImageFormat.Png); pbPng = msPng.ToArray(); } } } img.Dispose(); PwUuid pwUuid = null; int iEx = pd.GetCustomIconIndex(pbPng); if(iEx >= 0) pwUuid = pd.CustomIcons[iEx].Uuid; else { pwUuid = new PwUuid(true); pd.CustomIcons.Add(new PwCustomIcon(pwUuid, pbPng)); pd.UINeedsIconUpdate = true; pd.Modified = true; } pe.CustomIconUuid = pwUuid; } catch(Exception) { Debug.Assert(false); } }
/// <summary> /// Get the icon as an <c>Image</c> (original size). /// </summary> public Android.Graphics.Bitmap GetImage() { const long lKey = -1; Android.Graphics.Bitmap img; if (m_dImageCache.TryGetValue(lKey, out img)) { return(img); } try { img = GfxUtil.LoadImage(m_pbImageDataPng); } catch (Exception) { Debug.Assert(false); } m_dImageCache[lKey] = img; return(img); }
public static BinaryDataClass ClassifyData(byte[] pbData) { if (pbData == null) { Debug.Assert(false); throw new ArgumentNullException("pbData"); } try { Image img = GfxUtil.LoadImage(pbData); if (img != null) { img.Dispose(); return(BinaryDataClass.Image); } } catch (Exception) { } return(BinaryDataClass.Unknown); }
private bool IsValidImage(byte[] data) { // Invalid data if (data == null) { return(false); } try { Image image = GfxUtil.LoadImage(data); return(true); } catch (Exception) { // Invalid image format } return(false); }
private void OnBtnCustomAdd(object sender, EventArgs e) { string strAllSupportedFilter = KPRes.AllSupportedFiles + @" (*.bmp; *.emf; *.gif; *.ico; *.jpg; *.jpe; *.jpeg; *.jfif; *.jfi; *.jif; *.png; *.tif; *.tiff; *.wmf)" + @"|*.bmp;*.emf;*.gif;*.ico;*.jpg;*.jpe;*.jpeg;*.jfif;*.jfi;*.jif;*.png;*.tif;*.tiff;*.wmf"; StringBuilder sbFilter = new StringBuilder(); sbFilter.Append(strAllSupportedFilter); AddFileType(sbFilter, "*.bmp", "Windows Bitmap (*.bmp)"); AddFileType(sbFilter, "*.emf", "Windows Enhanced Metafile (*.emf)"); AddFileType(sbFilter, "*.gif", "Graphics Interchange Format (*.gif)"); AddFileType(sbFilter, "*.ico", "Windows Icon (*.ico)"); AddFileType(sbFilter, "*.jpg;*.jpe;*.jpeg;*.jfif;*.jfi;*.jif", "JPEG (*.jpg; *.jpe; *.jpeg; *.jfif; *.jfi; *.jif)"); AddFileType(sbFilter, "*.png", "Portable Network Graphics (*.png)"); AddFileType(sbFilter, "*.tif;*.tiff", "Tagged Image File Format (*.tif; *.tiff)"); AddFileType(sbFilter, "*.wmf", "Windows Metafile (*.wmf)"); sbFilter.Append(@"|" + KPRes.AllFiles + @" (*.*)|*.*"); OpenFileDialogEx ofd = UIUtil.CreateOpenFileDialog(KPRes.ImportFileTitle, sbFilter.ToString(), 1, null, true, AppDefs.FileDialogContext.Import); if (ofd.ShowDialog() == DialogResult.OK) { bool bSelectLastIcon = false; foreach (string strFile in ofd.FileNames) { bool bUnsupportedFormat = false; try { if (!File.Exists(strFile)) { throw new FileNotFoundException(); } // Image img = Image.FromFile(strFile); // Image img = Image.FromFile(strFile, false); // Image img = Bitmap.FromFile(strFile); // Bitmap img = new Bitmap(strFile); // Image img = Image.FromFile(strFile); byte[] pb = File.ReadAllBytes(strFile); // MemoryStream msSource = new MemoryStream(pb, false); // Image img = Image.FromStream(msSource); // msSource.Close(); Image img = GfxUtil.LoadImage(pb); MemoryStream ms = new MemoryStream(); if ((img.Width == 16) && (img.Height == 16)) { img.Save(ms, ImageFormat.Png); } else { // Image imgNew = new Bitmap(img, new Size(16, 16)); Bitmap imgSc = UIUtil.CreateScaledImage(img, 16, 16); imgSc.Save(ms, ImageFormat.Png); imgSc.Dispose(); } img.Dispose(); PwCustomIcon pwci = new PwCustomIcon(new PwUuid(true), ms.ToArray()); m_pwDatabase.CustomIcons.Add(pwci); ms.Close(); m_pwDatabase.UINeedsIconUpdate = true; m_pwDatabase.Modified = true; bSelectLastIcon = true; } catch (ArgumentException) { bUnsupportedFormat = true; } catch (System.Runtime.InteropServices.ExternalException) { bUnsupportedFormat = true; } catch (Exception exImg) { MessageService.ShowWarning(strFile, exImg); } if (bUnsupportedFormat) { MessageService.ShowWarning(strFile, KPRes.ImageFormatFeatureUnsupported); } } RecreateCustomIconList(bSelectLastIcon); } EnableControlsEx(); }
private void OnBtnCustomAdd(object sender, EventArgs e) { string strAllSupportedFilter = KPRes.AllSupportedFiles + @" (*.bmp; *.emf; *.gif; *.ico; *.jpg; *.jpe; *.jpeg; *.jfif; *.jfi; *.jif; *.png; *.tif; *.tiff; *.wmf)" + @"|*.bmp;*.emf;*.gif;*.ico;*.jpg;*.jpe;*.jpeg;*.jfif;*.jfi;*.jif;*.png;*.tif;*.tiff;*.wmf"; StringBuilder sbFilter = new StringBuilder(); sbFilter.Append(strAllSupportedFilter); AddFileType(sbFilter, "*.bmp", "Windows Bitmap (*.bmp)"); AddFileType(sbFilter, "*.emf", "Windows Enhanced Metafile (*.emf)"); AddFileType(sbFilter, "*.gif", "Graphics Interchange Format (*.gif)"); AddFileType(sbFilter, "*.ico", "Windows Icon (*.ico)"); AddFileType(sbFilter, "*.jpg;*.jpe;*.jpeg;*.jfif;*.jfi;*.jif", "JPEG (*.jpg; *.jpe; *.jpeg; *.jfif; *.jfi; *.jif)"); AddFileType(sbFilter, "*.png", "Portable Network Graphics (*.png)"); AddFileType(sbFilter, "*.tif;*.tiff", "Tagged Image File Format (*.tif; *.tiff)"); AddFileType(sbFilter, "*.wmf", "Windows Metafile (*.wmf)"); sbFilter.Append(@"|" + KPRes.AllFiles + @" (*.*)|*.*"); OpenFileDialogEx ofd = UIUtil.CreateOpenFileDialog(KPRes.ImportFileTitle, sbFilter.ToString(), 1, null, true, AppDefs.FileDialogContext.Import); if (ofd.ShowDialog() != DialogResult.OK) { return; } // Predicate<PwCustomIcon> fRequiresKdbx4p1 = delegate(PwCustomIcon ci) // { // return ((ci.Name.Length != 0) || ci.LastModificationTime.HasValue); // }; // bool bUseFileName = m_pd.CustomIcons.Exists(fRequiresKdbx4p1); PwUuid puSelect = PwUuid.Zero; foreach (string strFile in ofd.FileNames) { MemoryStream msPng = new MemoryStream(); string strError = null; try { byte[] pb = File.ReadAllBytes(strFile); using (Image img = GfxUtil.LoadImage(pb)) { if (img == null) { throw new FormatException(); } const int wMax = PwCustomIcon.MaxWidth; const int hMax = PwCustomIcon.MaxHeight; if ((img.Width <= wMax) && (img.Height <= hMax)) { img.Save(msPng, ImageFormat.Png); } else { using (Image imgSc = GfxUtil.ScaleImage(img, wMax, hMax)) { imgSc.Save(msPng, ImageFormat.Png); } } } PwUuid pu = new PwUuid(true); PwCustomIcon ci = new PwCustomIcon(pu, msPng.ToArray()); // if(bUseFileName) // { // string strName = UrlUtil.StripExtension( // UrlUtil.GetFileName(strFile)); // if(!string.IsNullOrEmpty(strName)) ci.Name = strName; // } m_pd.CustomIcons.Add(ci); m_pd.UINeedsIconUpdate = true; m_pd.Modified = true; if (puSelect.Equals(PwUuid.Zero)) { puSelect = pu; } } catch (ArgumentException) { strError = KPRes.ImageFormatFeatureUnsupported; } catch (System.Runtime.InteropServices.ExternalException) { strError = KPRes.ImageFormatFeatureUnsupported; } catch (Exception ex) { strError = ex.Message; } finally { msPng.Close(); } if (!string.IsNullOrEmpty(strError)) { MessageService.ShowWarning(strFile, strError); } } RecreateCustomIconList(puSelect); EnableControlsEx(); UIUtil.SetFocus(m_lvCustomIcons, this); }
private bool ResizeImage(ref byte[] data) { // Invalid data if (data == null) { return(false); } // KeePassLib.PwCustomIcon // Recommended maximum sizes, not obligatory //const int MaxWidth = 128; //const int MaxHeight = 128; int MaxWidth, MaxHeight; MaxWidth = MaxHeight = YetAnotherFaviconDownloaderExt.Config.GetMaximumIconSize(); Image image; try { image = GfxUtil.LoadImage(data); } catch (Exception) { // Invalid image format return(false); } // Checks if we need to resize if (image.Width <= MaxWidth && image.Height <= MaxHeight) { // don't need to resize the image // data = (original image) return(true); } // Try to resize the image to png try { double ratioWidth = MaxWidth / (double)image.Width; double ratioHeight = MaxHeight / (double)image.Height; double ratioImage = Math.Min(ratioHeight, ratioWidth); int h = (int)Math.Round(image.Height * ratioImage); int w = (int)Math.Round(image.Width * ratioImage); image = GfxUtil.ScaleImage(image, w, h); using (var ms = new MemoryStream()) { image.Save(ms, System.Drawing.Imaging.ImageFormat.Png); // If it's all ok // data = (resized image) data = ms.ToArray(); return(true); } } catch (Exception) { // Can't resize image // data = (original image) return(true); } }
public void LoadImageJunkTest() { var junkItems = new byte[] { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }; Assert.Null(GfxUtil.LoadImage(junkItems)); }
public void LoadImageNegativeTest() { Assert.Null(GfxUtil.LoadImage(null)); }
private static void ImportIcon(XmlNode xn, PwEntry pe, PwDatabase pd) { XmlNode xnIcon = xn.Attributes.GetNamedItem("ICON"); if (xnIcon == null) { return; } string strIcon = xnIcon.Value; if (!StrUtil.IsDataUri(strIcon)) { Debug.Assert(false); return; } try { byte[] pbImage = StrUtil.DataUriToData(strIcon); if ((pbImage == null) || (pbImage.Length == 0)) { Debug.Assert(false); return; } Image img = GfxUtil.LoadImage(pbImage); if (img == null) { Debug.Assert(false); return; } byte[] pbPng; if ((img.Width == 16) && (img.Height == 16)) { MemoryStream msPng = new MemoryStream(); img.Save(msPng, ImageFormat.Png); pbPng = msPng.ToArray(); msPng.Close(); } else { Bitmap bmp = UIUtil.CreateScaledImage(img, 16, 16); MemoryStream msPng = new MemoryStream(); bmp.Save(msPng, ImageFormat.Png); pbPng = msPng.ToArray(); msPng.Close(); bmp.Dispose(); } img.Dispose(); PwUuid pwUuid = null; int iEx = pd.GetCustomIconIndex(pbPng); if (iEx >= 0) { pwUuid = pd.CustomIcons[iEx].Uuid; } else { pwUuid = new PwUuid(true); pd.CustomIcons.Add(new PwCustomIcon(pwUuid, pbPng)); pd.UINeedsIconUpdate = true; pd.Modified = true; } pe.CustomIconUuid = pwUuid; } catch (Exception) { Debug.Assert(false); } }