static private Image GetBitmapFromResource(Type t, string bitmapname, bool large) { if (bitmapname == null) { return(null); } Image img = null; // load the image from the manifest resources. // Stream stream = BitmapSelector.GetResourceStream(t, bitmapname); if (stream != null) { Bitmap b = new Bitmap(stream); img = b; MakeBackgroundAlphaZero(b); if (large) { img = new Bitmap(b, largeSize.Width, largeSize.Height); } if (DpiHelper.IsScalingRequired) { b = (Bitmap)img; DpiHelper.ScaleBitmapLogicalToDevice(ref b); img = b; } } return(img); }
// Just forwards to Image.FromFile eating any non-critical exceptions that may result. private static Image?GetImageFromFile(string?imageFile, bool large, bool scaled = true) { Image?image = null; try { if (imageFile != null) { string?ext = Path.GetExtension(imageFile); if (ext != null && string.Equals(ext, ".ico", StringComparison.OrdinalIgnoreCase)) { //ico files support both large and small, so we respect the large flag here. using (FileStream reader = File.OpenRead(imageFile !)) { image = GetIconFromStream(reader, large, scaled); } } else if (!large) { //we only read small from non-ico files. image = Image.FromFile(imageFile !); Bitmap?b = image as Bitmap; if (DpiHelper.IsScalingRequired && scaled) { DpiHelper.ScaleBitmapLogicalToDevice(ref b); } } } } catch (Exception e) when(!ClientUtils.IsCriticalException(e)) { } return(image); }
/// <include file='doc\ToolboxItem.uex' path='docs/doc[@for="ToolboxItem.ToolboxItem"]/*' /> /// <devdoc> /// Initializes a new instance of the ToolboxItem class. /// </devdoc> public ToolboxItem() { if (!isScalingInitialized) { if (DpiHelper.IsScalingRequired) { iconWidth = DpiHelper.LogicalToDeviceUnitsX(ICON_DIMENSION); iconHeight = DpiHelper.LogicalToDeviceUnitsY(ICON_DIMENSION); } isScalingInitialized = true; } }
// Just forwards to Image.FromFile eating any non-critical exceptions that may result. private static Image GetImageFromFile(string imageFile, bool large, bool scaled = true) { Image image = null; try { if (imageFile != null) { imageFile = GetFileNameFromBitmapSelector(imageFile); string ext = Path.GetExtension(imageFile); if (ext != null && string.Equals(ext, ".ico", StringComparison.OrdinalIgnoreCase)) { //ico files support both large and small, so we respect the large flag here. FileStream reader = System.IO.File.Open(imageFile, FileMode.Open); if (reader != null) { try { image = GetIconFromStream(reader, large, scaled); } finally { reader.Close(); } } } else if (!large) { //we only read small from non-ico files. image = Image.FromFile(imageFile); Bitmap b = image as Bitmap; if (DpiHelper.IsScalingRequired && scaled) { DpiHelper.ScaleBitmapLogicalToDevice(ref b); } } } } catch (Exception e) { if (ClientUtils.IsCriticalException(e)) { throw; } Debug.Fail("Failed to load toolbox image '" + imageFile + "':\r\n" + e.ToString()); } return(image); }
private static Image GetIconFromStream(Stream stream, bool large) { if (stream == null) { return(null); } Icon ico = new Icon(stream); Icon sizedico = new Icon(ico, large ? largeSize : smallSize); Bitmap b = sizedico.ToBitmap(); if (DpiHelper.IsScalingRequired) { DpiHelper.ScaleBitmapLogicalToDevice(ref b); } return(b); }