/// <summary> /// Convert Bitmap to BitmapImage /// </summary> /// <param name="input"></param> /// <returns></returns> public static System.Windows.Media.Imaging.BitmapImage Bitmap2Bitmapimage(System.Drawing.Bitmap input) { System.Windows.Media.Imaging.BitmapImage retval = new System.Windows.Media.Imaging.BitmapImage(); if (input == null) { return(retval); } IntPtr hBitmap = input.GetHbitmap(); try { using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) { input.Save(ms, System.Drawing.Imaging.ImageFormat.Png); ms.Position = 0; retval.BeginInit(); // According to MSDN, "The default OnDemand cache option retains access to the stream until the image is needed." // Force the bitmap to load right now so we can dispose the stream. retval.CacheOption = System.Windows.Media.Imaging.BitmapCacheOption.OnLoad; retval.StreamSource = ms; retval.EndInit(); retval.Freeze(); } } catch (System.Exception ex) { Console.WriteLine("In ImageTypeConverter.Bitmap2Bitmapimage" + ex.Message); } return(retval); }
/// <summary> /// Gets the WPF bitmap image. /// </summary> internal System.Windows.Media.Imaging.BitmapImage GetWpfBitmapImage(IconCollectionInfo collectionInfo, IconFileInfo fileInfo) { string iconFileKey = GetIconFileKey(collectionInfo, fileInfo); // Load the ImageSource (me may have cached it before System.Windows.Media.Imaging.BitmapImage result = null; if (!m_wpfBitmapImages.TryGetValue(iconFileKey, out result)) { // Try to load the icon from png file var pngIconLink = TryFindPngIcon(collectionInfo, fileInfo); if (pngIconLink != null) { using (Stream inStream = pngIconLink.OpenRead()) { result = new System.Windows.Media.Imaging.BitmapImage(); result.BeginInit(); result.StreamSource = inStream; result.EndInit(); result.Freeze(); } } m_wpfBitmapImages.Add(iconFileKey, result); } return(result); }
private BitmapImage GetMonsterIcon(string MonsterEm) { if (!System.IO.File.Exists($@"HunterPie.Resources\Monsters\Icons\{MonsterEm}.png")) { return(null); } Uri ImageURI = new Uri($@"HunterPie.Resources\Monsters\Icons\{MonsterEm}.png", UriKind.Relative); BitmapImage mIcon = new BitmapImage(ImageURI); mIcon.Freeze(); return(mIcon); }
private BitmapImage GetMonsterIcon(string MonsterEm) { if (!File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $@"HunterPie.Resources\Monsters\Icons\{MonsterEm}.png"))) { return(null); } Uri ImageURI = new Uri(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $@"HunterPie.Resources\Monsters\Icons\{MonsterEm}.png"), UriKind.Absolute); BitmapImage mIcon = new BitmapImage(ImageURI); mIcon.Freeze(); return(mIcon); }
// http://stackoverflow.com/a/3427387/1761622 public static Tuple <ImageSource, System.Drawing.Image> CreateImageSource(System.Drawing.Image image) { var bitmap = new System.Windows.Media.Imaging.BitmapImage(); bitmap.BeginInit(); MemoryStream memoryStream = new MemoryStream(); image.Save(memoryStream, ImageFormat.Bmp); memoryStream.Seek(0, System.IO.SeekOrigin.Begin); bitmap.StreamSource = memoryStream; bitmap.EndInit(); bitmap.Freeze(); return(Tuple.Create <ImageSource, System.Drawing.Image>(bitmap, image)); }
public static System.Windows.Media.Imaging.BitmapImage GetImage(string imagePath) { System.Windows.Media.Imaging.BitmapImage bitmap = new System.Windows.Media.Imaging.BitmapImage(); if (System.IO.File.Exists(imagePath)) { bitmap.BeginInit(); bitmap.CacheOption = System.Windows.Media.Imaging.BitmapCacheOption.OnLoad; using (Stream ms = new MemoryStream(System.IO.File.ReadAllBytes(imagePath))) { bitmap.StreamSource = ms; bitmap.EndInit(); bitmap.Freeze(); } } return(bitmap); }
public static System.Windows.Media.Imaging.BitmapImage ToBitmapImage(System.Drawing.Bitmap bitmap) { using (var memory = new System.IO.MemoryStream()) { bitmap.Save(memory, System.Drawing.Imaging.ImageFormat.Png); memory.Position = 0; var bitmapImage = new System.Windows.Media.Imaging.BitmapImage(); bitmapImage.BeginInit(); bitmapImage.StreamSource = memory; bitmapImage.CacheOption = System.Windows.Media.Imaging.BitmapCacheOption.OnLoad; bitmapImage.EndInit(); bitmapImage.Freeze(); return(bitmapImage); } }
/// <summary> /// Convert WriteableBitmap to BitmapImage. /// refer to https://stackoverflow.com/questions/14161665/how-do-i-convert-a-writeablebitmap-object-to-a-bitmapimage-object-in-wpf /// </summary> /// <param name="wbm"></param> /// <returns></returns> public static System.Windows.Media.Imaging.BitmapImage ConvertWriteablebitmapToBitmapimage(System.Windows.Media.Imaging.WriteableBitmap wbm) { System.Windows.Media.Imaging.BitmapImage bmpimg = new System.Windows.Media.Imaging.BitmapImage(); using (System.IO.MemoryStream stream = new System.IO.MemoryStream()) { System.Windows.Media.Imaging.PngBitmapEncoder encoder = new System.Windows.Media.Imaging.PngBitmapEncoder(); encoder.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(wbm)); encoder.Save(stream); bmpimg.BeginInit(); bmpimg.CacheOption = System.Windows.Media.Imaging.BitmapCacheOption.OnLoad; bmpimg.StreamSource = stream; bmpimg.EndInit(); bmpimg.Freeze(); } return(bmpimg); }
private static ImageSource BitmapToImageSource(Bitmap bitmap) { MemoryStream ms = new MemoryStream(); ImageSource image = null; bitmap.Save(ms, ImageFormat.Png); ms.Position = 0; BitmapImage bi = new BitmapImage(); bi.BeginInit(); bi.StreamSource = ms; bi.EndInit(); bi.Freeze(); Dispatcher.CurrentDispatcher.Invoke(() => image = bi); return(image); }
public static System.Windows.Media.Imaging.BitmapSource ToWpf(this System.Drawing.Bitmap bitmap) { using (var stream = new System.IO.MemoryStream()) { var imgFormat = bitmap.PixelFormat == System.Drawing.Imaging.PixelFormat.Format24bppRgb ? System.Drawing.Imaging.ImageFormat.Bmp : System.Drawing.Imaging.ImageFormat.Png; bitmap.Save(stream, imgFormat); stream.Position = 0; var result = new System.Windows.Media.Imaging.BitmapImage(); result.BeginInit(); // According to MSDN, "The default OnDemand cache option retains access to the stream until the image is needed." // Force the bitmap to load right now so we can dispose the stream. result.CacheOption = System.Windows.Media.Imaging.BitmapCacheOption.OnLoad; result.StreamSource = stream; result.EndInit(); result.Freeze(); return(result); } }
public static System.Windows.Media.Imaging.BitmapImage ToWpfBitmap(System.Drawing.Bitmap bitmap) { using (MemoryStream stream = new MemoryStream()) { bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png); stream.Position = 0; System.Windows.Media.Imaging.BitmapImage result = new System.Windows.Media.Imaging.BitmapImage(); result.BeginInit(); result.DecodePixelWidth = 16; // According to MSDN, "The default OnDemand cache option retains access to the stream until the image is needed." // Force the bitmap to load right now so we can dispose the stream. result.CacheOption = System.Windows.Media.Imaging.BitmapCacheOption.OnLoad; result.StreamSource = stream; result.EndInit(); result.Freeze(); return(result); } }
public static Media.Imaging.BitmapImage ToBitmapImage(this Drawing.Bitmap bitmap, int PixelWidth = 0, int PixelHeight = 0) { using (var memory = new MemoryStream()) { bitmap.Save(memory, Drawing.Imaging.ImageFormat.Png); memory.Position = 0; var bitmapImage = new Media.Imaging.BitmapImage(); bitmapImage.BeginInit(); bitmapImage.StreamSource = memory; bitmapImage.CacheOption = Media.Imaging.BitmapCacheOption.OnLoad; bitmapImage.DecodePixelWidth = PixelWidth; bitmapImage.DecodePixelHeight = PixelHeight; bitmapImage.EndInit(); bitmapImage.Freeze(); return(bitmapImage); } }
//Parse byte[] to Image and write to Clipboard private void CopyClipboard(byte[] cimg) { //Parse byte[] to Images var image = new System.Windows.Media.Imaging.BitmapImage(); using (var mem = new MemoryStream(cimg)) { mem.Position = 0; image.BeginInit(); image.CreateOptions = System.Windows.Media.Imaging.BitmapCreateOptions.PreservePixelFormat; image.CacheOption = System.Windows.Media.Imaging.BitmapCacheOption.OnLoad; image.UriSource = null; image.StreamSource = mem; image.EndInit(); } image.Freeze(); //Copy whole Image to Clipboard Clipboard.SetImage(image); }
/// <summary> /// 将二进制流转换成图片 /// </summary> /// <param name="byteArray"></param> /// <returns></returns> public static System.Windows.Media.Imaging.BitmapImage ByteArrayToBitmapImage(this byte[] byteArray) { System.Windows.Media.Imaging.BitmapImage bmp = null; try { bmp = new System.Windows.Media.Imaging.BitmapImage(); bmp.BeginInit(); bmp.StreamSource = new System.IO.MemoryStream(byteArray); bmp.EndInit(); bmp.Freeze(); } catch { bmp = null; } return(bmp); }
private System.Windows.Media.ImageSource ToBitmapsource(Bitmap bitmap) { using (MemoryStream stream = new MemoryStream()) { bitmap.Save(stream, ImageFormat.Png); // 坑点:格式选Bmp时,不带透明度 stream.Position = 0; System.Windows.Media.Imaging.BitmapImage result = new System.Windows.Media.Imaging.BitmapImage(); result.BeginInit(); // According to MSDN, "The default OnDemand cache option retains access to the stream until the image is needed." // Force the bitmap to load right now so we can dispose the stream. result.CacheOption = System.Windows.Media.Imaging.BitmapCacheOption.OnLoad; result.StreamSource = stream; result.EndInit(); result.Freeze(); stream.Flush(); stream.Dispose(); bitmap.Dispose(); return(result); } }
public WarningTriangle() { this.InitializeComponent(); // Loading embedded image resource in this way as the simplest way of doing this I could find. var bmp = RapidXamlToolkit.Resources.ImageResources.WarningIcon; using (var memory = new System.IO.MemoryStream()) { bmp.Save(memory, System.Drawing.Imaging.ImageFormat.Png); memory.Position = 0; var bitmapImage = new System.Windows.Media.Imaging.BitmapImage(); bitmapImage.BeginInit(); bitmapImage.StreamSource = memory; bitmapImage.CacheOption = System.Windows.Media.Imaging.BitmapCacheOption.OnLoad; bitmapImage.EndInit(); bitmapImage.Freeze(); this.WarningIconImage.Source = bitmapImage; } }
public object this[string relativeFileName] { get { if (_contentManager._bookContent.Images.TryGetValue(relativeFileName, out var contentFile)) { using (var stream = new MemoryStream(contentFile.Content)) { var bitmap = new System.Windows.Media.Imaging.BitmapImage(); bitmap.BeginInit(); bitmap.StreamSource = stream; bitmap.CacheOption = System.Windows.Media.Imaging.BitmapCacheOption.OnLoad; bitmap.EndInit(); bitmap.Freeze(); return(bitmap); } } else { return(null); } } }
private static System.Windows.Media.Imaging.BitmapImage GetBitmap(List <RequestedTileInformation> tis) { System.Windows.Media.Imaging.BitmapImage result = null; bool valid = true; if (tis[0].Zoom == tis[0].mapFile.MapFileHandler.MapControlFactory.LastRequestedZoomLevel) { using (System.Drawing.Bitmap img = new System.Drawing.Bitmap(256, 256)) { img.SetResolution(96, 96); using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(img)) { g.SmoothingMode = SmoothingMode.AntiAlias; g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit; g.Clear(System.Drawing.ColorTranslator.FromHtml("#f1eee8")); valid = valid && drawWays(tis, g); valid = valid && drawPOIs(tis, g); } if (valid) { using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) { img.Save(ms, System.Drawing.Imaging.ImageFormat.Png); ms.Position = 0; result = new System.Windows.Media.Imaging.BitmapImage(); result.BeginInit(); result.CacheOption = System.Windows.Media.Imaging.BitmapCacheOption.OnLoad; result.StreamSource = ms; result.EndInit(); result.Freeze(); } } } } return(result); }
private static System.Windows.Media.Imaging.BitmapImage GetBitmap(List<RequestedTileInformation> tis) { System.Windows.Media.Imaging.BitmapImage result = null; bool valid = true; if (tis[0].Zoom == tis[0].mapFile.MapFileHandler.MapControlFactory.LastRequestedZoomLevel) { using (System.Drawing.Bitmap img = new System.Drawing.Bitmap(256, 256)) { img.SetResolution(96, 96); using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(img)) { g.SmoothingMode = SmoothingMode.AntiAlias; g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit; g.Clear(System.Drawing.ColorTranslator.FromHtml("#f1eee8")); valid = valid && drawWays(tis, g); valid = valid && drawPOIs(tis, g); } if (valid) { using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) { img.Save(ms, System.Drawing.Imaging.ImageFormat.Png); ms.Position = 0; result = new System.Windows.Media.Imaging.BitmapImage(); result.BeginInit(); result.CacheOption = System.Windows.Media.Imaging.BitmapCacheOption.OnLoad; result.StreamSource = ms; result.EndInit(); result.Freeze(); } } } } return result; }
/// <summary> /// Creates an ImageList object for the given type of icons. /// </summary> /// <param name="sordTypes">SordType array</param> /// <param name="iconType">Icon type: 0=normal, 1=reference, 2=workflow</param> /// protected void makeIcons(SordType[] sordTypes, int iconType) { foreach (SordType s in sordTypes) { System.Drawing.Icon icon = null; if (s.icon != null) { byte[] data = null; switch (iconType) { case 0: data = s.icon.data; break; case 1: data = s.disabledIcon.data; break; case 2: data = s.workflowIcon.data; break; } try { bool existsImgData = (data != null && data.Length > 0); if (existsImgData) { #if NET40 using (MemoryStream imgMemoryStream = new MemoryStream(data, 0, data.Length, false, true)) { try { // Creating wpf image System.Windows.Media.Imaging.BitmapImage bitmapImage = new System.Windows.Media.Imaging.BitmapImage(); bitmapImage.BeginInit(); bitmapImage.DecodePixelWidth = 16; bitmapImage.CacheOption = System.Windows.Media.Imaging.BitmapCacheOption.OnLoad; bitmapImage.StreamSource = imgMemoryStream; bitmapImage.EndInit(); bitmapImage.Freeze(); wpfImageList.Add(bitmapImage); } catch (Exception) { } } #endif using (MemoryStream imgMemoryStream = new MemoryStream(data, 0, data.Length, false, true)) { try { icon = new System.Drawing.Icon(imgMemoryStream, 16, 16); imageList.Images.Add(icon); if (iconType == 0) { mapTypeToImageIdx[s.id] = imageList.Images.Count - 1; } } catch (Exception) { } } } } catch (Exception) { } } } }
private static void DrawTextVariables(JArray AssetProperties) { DrawDisplayName(_displayName); DrawDescription(_description); switch (AssetsLoader.ExportType) { case "AthenaBackpackItemDefinition": case "AthenaBattleBusItemDefinition": case "AthenaCharacterItemDefinition": case "AthenaConsumableEmoteItemDefinition": case "AthenaSkyDiveContrailItemDefinition": case "AthenaDanceItemDefinition": case "AthenaEmojiItemDefinition": case "AthenaGliderItemDefinition": case "AthenaItemWrapDefinition": case "AthenaLoadingScreenItemDefinition": case "AthenaMusicPackItemDefinition": case "AthenaPetCarrierItemDefinition": case "AthenaPickaxeItemDefinition": case "AthenaSprayItemDefinition": case "AthenaToyItemDefinition": case "AthenaVictoryPoseItemDefinition": case "FortBannerTokenType": DrawToBottom("Left", _shortDescription); DrawToBottom("Right", _cosmeticSource); break; case "FortWeaponRangedItemDefinition": case "AthenaGadgetItemDefinition": DrawToBottom("Left", _maxStackSize); DrawToBottom("Right", _itemAction); break; case "FortVariantTokenType": DrawToBottom("Left", _shortDescription); DrawToBottom("Right", _cosmeticItemDefinition); break; case "FortHeroType": HeroGameplayDefinition.GetHeroPerk(AssetProperties); break; } if (_userFacingFlagsToken != null) { foreach (JToken uFF in _userFacingFlagsToken) { IconUserFacingFlags.DrawUserFacingFlag(uFF); } IconUserFacingFlags.xCoords = 4 - 25; //reset uFF coords } if (_userHeroFlagsToken != null) { foreach (JToken uFF in _userHeroFlagsToken) { IconUserFacingFlags.DrawHeroFacingFlag(uFF); } IconUserFacingFlags.xCoords = 4 - 25; //reset uFF coords } if (_userWeaponFlagsToken != null) { foreach (JToken uFF in _userWeaponFlagsToken) { IconUserFacingFlags.DrawWeaponFacingFlag(uFF); } IconUserFacingFlags.xCoords = 4 - 25; //reset uFF coords } if (!string.IsNullOrEmpty(_miniMapIconBrushPath) && !_miniMapIconBrushPath.Contains("UI_Radar_EnemyDot_White")) { using (System.IO.Stream image = AssetsUtility.GetStreamImageFromPath(_miniMapIconBrushPath)) { if (image != null) { System.Windows.Media.Imaging.BitmapImage bmp = new System.Windows.Media.Imaging.BitmapImage(); bmp.BeginInit(); bmp.CacheOption = System.Windows.Media.Imaging.BitmapCacheOption.OnLoad; bmp.StreamSource = image; bmp.EndInit(); bmp.Freeze(); IconUserFacingFlags.xCoords += 25; IconCreator.ICDrawingContext.DrawImage(bmp, new Rect(IconUserFacingFlags.xCoords, 4, 25, 25)); IconUserFacingFlags.xCoords = 4 - 25; //reset uFF coords } } } }
internal void FetchImage() { if (BookImageUrl != null) { if (_imgcache == null) { try { System.Windows.Media.Imaging.BitmapImage bi = new System.Windows.Media.Imaging.BitmapImage(); bi.BeginInit(); var res = HttpWebRequest.Create(BookImageUrl).GetResponse(); var str = res.GetResponseStream(); bi.StreamSource = str; bi.EndInit(); bi.Freeze(); str.Close(); str.Dispose(); _imgcache = bi; BookImageFld = bi; } catch (Exception) { BookImageFld = null; } } else BookImageFld = _imgcache; } }
public static System.Windows.Media.Imaging.BitmapImage GetImage(string fileId) { if (string.IsNullOrWhiteSpace(fileId)) { if (_olaf == null) { _olaf = GetImage(Olaf); } return _olaf; } using (var db = new PetoeterDb(PetoeterDb.FileName)) { var file = db.FileStorage.FindById(fileId); if (file == null) { if (fileId == Olaf) { SaveFile(Olaf, @"olaf.png"); } return null; } using(var reader = file.OpenRead()) { System.IO.MemoryStream mem = new System.IO.MemoryStream(); mem.SetLength(file.Length); reader.Read(mem.GetBuffer(), 0, (int)file.Length); mem.Flush(); System.Windows.Media.Imaging.BitmapImage image = new System.Windows.Media.Imaging.BitmapImage(); image.BeginInit(); image.StreamSource = mem; image.EndInit(); image.Freeze(); return image; } } }