public static System.Windows.Media.Imaging.BitmapImage DrawingImageToBitmapImage(System.Drawing.Image dImage) { if (dImage == null) { return(null); } var image = new System.Windows.Media.Imaging.BitmapImage(); using (var stream = new MemoryStream()) { dImage.Save(stream, System.Drawing.Imaging.ImageFormat.Png); stream.Seek(0, SeekOrigin.Begin); image.BeginInit(); image.CreateOptions = System.Windows.Media.Imaging.BitmapCreateOptions.PreservePixelFormat; image.CacheOption = System.Windows.Media.Imaging.BitmapCacheOption.OnLoad; image.UriSource = null; image.StreamSource = stream; image.EndInit(); } image.Freeze(); return(image); }
public BitmapImage SetData(byte[] data) { OriginalData = data; var bi = new BitmapImage(); using (var ms = new MemoryStream(data)) { using (var b = new Bitmap(ms)) { var decoders = ImageCodecInfo.GetImageDecoders(); foreach (var ici in decoders) { if (ici.FormatID == b.RawFormat.Guid) { FileExtension = ici.FilenameExtension.Split(';')[0].ToLower(); } } } ms.Seek(0, SeekOrigin.Begin); bi.BeginInit(); bi.CacheOption = BitmapCacheOption.OnLoad; bi.StreamSource = ms; bi.EndInit(); bi.Freeze(); } Bitmap = bi; return bi; }
public async Task<ImageSource> DownloadPicture(string imageUri) { var request = (HttpWebRequest)WebRequest.Create(imageUri); if (string.IsNullOrEmpty(Configuration.SessionCookies) == false) { request.CookieContainer = new CookieContainer(); request.CookieContainer.SetCookies(request.RequestUri, Configuration.SessionCookies); } var response = (HttpWebResponse)(await request.GetResponseAsync()); using (Stream inputStream = response.GetResponseStream()) using (Stream outputStream = new MemoryStream()) { var buffer = new byte[4096]; int bytesRead; do { bytesRead = await inputStream.ReadAsync(buffer, 0, buffer.Length); outputStream.Write(buffer, 0, bytesRead); } while (bytesRead != 0); var bitmapImage = new BitmapImage(); bitmapImage.BeginInit(); bitmapImage.CacheOption = BitmapCacheOption.OnLoad; bitmapImage.StreamSource = outputStream; bitmapImage.EndInit(); bitmapImage.Freeze(); return bitmapImage; } }
public SignatureWindow(string signature) { _digitalSignatureCollection.AddRange(Settings.Instance.Global_DigitalSignatures.ToArray()); InitializeComponent(); { var icon = new BitmapImage(); icon.BeginInit(); icon.StreamSource = new FileStream(Path.Combine(_serviceManager.Paths["Icons"], "Amoeba.ico"), FileMode.Open, FileAccess.Read, FileShare.Read); icon.EndInit(); if (icon.CanFreeze) icon.Freeze(); this.Icon = icon; } _signatureComboBox_CollectionContainer.Collection = _digitalSignatureCollection; if (_digitalSignatureCollection.Count > 0) _signatureComboBox.SelectedIndex = 1; if (signature != null) { for (int index = 0; index < Settings.Instance.Global_DigitalSignatures.Count; index++) { if (Settings.Instance.Global_DigitalSignatures[index].ToString() == signature) { _signatureComboBox.SelectedIndex = index + 1; break; } } } }
public void load_image(object arg, DoWorkEventArgs e) { int contribution_id = (int)e.Argument; if (!window_manager.downloaded_contributions.Contains(contribution_id)) { naturenet_dataclassDataContext db = new naturenet_dataclassDataContext(); var result1 = from c in db.Contributions where c.id == contribution_id select c; if (result1.Count() != 0) { Contribution contrib = result1.First<Contribution>(); bool result = file_manager.download_file_from_googledirve(contrib.media_url, contribution_id); if (result) window_manager.downloaded_contributions.Add(contribution_id); } } try { ImageSource src = new BitmapImage(new Uri(configurations.GetAbsoluteContributionPath() + contribution_id.ToString() + ".jpg")); src.Freeze(); the_image = src; //window_manager.contributions.Add(contribution_id, src); e.Result = (object)contribution_id; } catch (Exception) { /// write log e.Result = -1; } }
public async Task<BitmapImage> BitmapImageAsync(string url) { Stream stream = null; HttpClient WebClient = new HttpClient(); BitmapImage image = new BitmapImage(); try { stream = new MemoryStream(await WebClient.GetByteArrayAsync(url)); image.BeginInit(); image.CacheOption = BitmapCacheOption.OnLoad; // here image.StreamSource = stream; image.EndInit(); image.Freeze(); } catch { } if (stream != null) { stream.Close(); stream.Dispose(); stream = null; } url = null; WebClient.CancelPendingRequests(); WebClient.Dispose(); WebClient = null; return image; }
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { List<WPFMitsuControls.BookPage> pages = new List<WPFMitsuControls.BookPage>(); var coll = (System.Collections.ObjectModel.Collection<string>)value; foreach (var by in coll) { try { var img = new Image(); var bitmp = new BitmapImage(); bitmp.BeginInit(); bitmp.UriSource = new Uri(by); bitmp.CacheOption = BitmapCacheOption.None; bitmp.EndInit(); bitmp.Freeze(); img.Source = bitmp; WPFMitsuControls.BookPage bp = new WPFMitsuControls.BookPage(); bp.Content = new Grid(); ((Grid)bp.Content).Children.Add(img); pages.Add(bp); } catch (Exception) { } } return pages; }
public async Task<BitmapImage> DownloadDataAsync(string uri, string referer = null) { using (var wc = new WebClient {Referer = referer}) { OriginalData = await wc.DownloadDataTaskAsync(uri); } var bi = new BitmapImage(); using (var ms = new MemoryStream(OriginalData)) { using (var b = new Bitmap(ms)) { var decoders = ImageCodecInfo.GetImageDecoders(); foreach (var ici in decoders) { if (ici.FormatID == b.RawFormat.Guid) { FileExtension = ici.FilenameExtension.Split(';')[0].ToLower(); } } } ms.Seek(0, SeekOrigin.Begin); bi.BeginInit(); bi.CacheOption = BitmapCacheOption.OnLoad; bi.StreamSource = ms; bi.EndInit(); bi.Freeze(); } Bitmap = bi; return bi; }
/// <summary> /// 将图片文件转换成BitmapSource /// </summary> /// <param name="sFilePath"></param> /// <returns></returns> public static System.Windows.Media.Imaging.BitmapSource ToBitmapSource(string sFilePath) { try { using (System.IO.FileStream fs = new System.IO.FileStream(sFilePath, System.IO.FileMode.Open, System.IO.FileAccess.Read)) { byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); fs.Close(); fs.Dispose(); System.Windows.Media.Imaging.BitmapImage bitmapImage = new System.Windows.Media.Imaging.BitmapImage(); using (System.IO.MemoryStream ms = new System.IO.MemoryStream(buffer)) { bitmapImage.BeginInit(); bitmapImage.StreamSource = ms; bitmapImage.CacheOption = System.Windows.Media.Imaging.BitmapCacheOption.OnLoad; bitmapImage.EndInit(); bitmapImage.Freeze(); } return(bitmapImage); } } catch (Exception ex) { return(null); } }
public static System.Windows.Media.Imaging.BitmapImage ToImage(byte[] bytes) { //if (testCount++ > 10) { testCount = 0; throw new Exception("TestException"); } try { using (Stream ms = new System.IO.MemoryStream(bytes)) { System.Windows.Media.Imaging.BitmapImage image = null; try { System.Windows.Application.Current.Dispatcher.Invoke((Action)(() => { try { image = new System.Windows.Media.Imaging.BitmapImage(); image.BeginInit(); image.CacheOption = System.Windows.Media.Imaging.BitmapCacheOption.OnLoad; image.StreamSource = ms; image.EndInit(); image.Freeze(); } catch (Exception) { } })); } catch (NullReferenceException) { } return(image); } } catch (Exception e) { } return(null); }
private Image createImage(Node node, int children) { Image img = new Image(); BitmapImage bi = new BitmapImage(); bi.BeginInit(); bi.UriSource = new Uri("res/img/" + node.ImageName + ".png", UriKind.Relative); bi.EndInit(); bi.Freeze(); img.Source = bi; if (node.Level == 1) { img.Height = 118; img.Width = 258; } else { if (children <= 6) { img.Height = 200; img.Width = 209; } else { img.Height = 150; img.Width = 153; } } img.Margin = new Thickness(10, 0, 80, 0); img.Tag = node; img.MouseLeftButtonDown += gotoNext; return img; }
public BitmapImage Convert([CanBeNull] string id) { if (id == null) id = @"_"; BitmapImage bi; if (Cache.TryGetValue(id, out bi)) return bi; if (_archive == null) { _archive = new ZipArchive(new MemoryStream(BinaryResources.Flags)); } var entryStream = (_archive.GetEntry(id) ?? _archive.GetEntry(@"_"))?.Open(); if (entryStream == null) { return null; } bi = new BitmapImage(); bi.BeginInit(); bi.CacheOption = BitmapCacheOption.OnLoad; bi.StreamSource = entryStream.ReadAsMemoryStream(); bi.EndInit(); bi.Freeze(); Cache[id] = bi; return bi; }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value == null) return null; if (value.ToString().IsEmpty() || (value.ToString() == "none")) { var image = new BitmapImage(); image.BeginInit(); image.UriSource = new Uri("pack://application:,,,/PCSX2Bonus;component/Images/noart.png", UriKind.Absolute); image.DecodePixelWidth = 0x109; image.DecodePixelHeight = 350; image.EndInit(); if (image.CanFreeze) image.Freeze(); return image; } var image2 = new BitmapImage(); using (var stream = File.OpenRead(value.ToString())) { image2.BeginInit(); image2.StreamSource = stream; image2.DecodePixelWidth = 0x109; image2.DecodePixelHeight = 350; image2.CacheOption = BitmapCacheOption.OnLoad; image2.EndInit(); } if (image2.CanFreeze) image2.Freeze(); return image2; }
public BoxEditWindow(Box box) { _box = box; InitializeComponent(); _nameTextBox.MaxLength = Box.MaxNameLength; { var icon = new BitmapImage(); icon.BeginInit(); icon.StreamSource = new FileStream(Path.Combine(_serviceManager.Paths["Icons"], "Amoeba.ico"), FileMode.Open, FileAccess.Read, FileShare.Read); icon.EndInit(); if (icon.CanFreeze) icon.Freeze(); this.Icon = icon; } lock (_box.ThisLock) { _nameTextBox.Text = _box.Name; } _nameTextBox.TextChanged += _nameTextBox_TextChanged; _nameTextBox_TextChanged(null, null); }
public static Media.Brush GetThumbnailBrush(string fileName, int width = 256, int height = 256, ThumbnailOptions options = ThumbnailOptions.None) { IntPtr hBitmap = GetHBitmap(Path.GetFullPath(fileName), width, height, options); try { Bitmap bmp = Bitmap.FromHbitmap(hBitmap); Media.Imaging.BitmapImage img = new Media.Imaging.BitmapImage(); using (var ms = new MemoryStream()) { bmp.Save(ms, ImageFormat.Png); img.BeginInit(); img.StreamSource = ms; img.CacheOption = Media.Imaging.BitmapCacheOption.OnLoad; img.EndInit(); img.Freeze(); } return(new Media.ImageBrush(img)); } finally { // delete HBitmap to avoid memory leaks DeleteObject(hBitmap); } }
public SignatureWindow(string signature) { var digitalSignatureCollection = new List<object>(); digitalSignatureCollection.Add(new ComboBoxItem() { Content = "" }); digitalSignatureCollection.AddRange(Settings.Instance.Global_DigitalSignatureCollection.Select(n => new DigitalSignatureComboBoxItem(n)).ToArray()); InitializeComponent(); { var icon = new BitmapImage(); icon.BeginInit(); icon.StreamSource = new FileStream(Path.Combine(App.DirectoryPaths["Icons"], "Amoeba.ico"), FileMode.Open, FileAccess.Read, FileShare.Read); icon.EndInit(); if (icon.CanFreeze) icon.Freeze(); this.Icon = icon; } _signatureComboBox.ItemsSource = digitalSignatureCollection; for (int index = 0; index < Settings.Instance.Global_DigitalSignatureCollection.Count; index++) { if (Settings.Instance.Global_DigitalSignatureCollection[index].ToString() == signature) { _signatureComboBox.SelectedIndex = index + 1; break; } } }
public AsyncImage(Uri final, Uri placeholder) { this.final = final; this.placeholder = placeholder; Source = new BitmapImage(placeholder); Task.Run(() => { BitmapImage img = new BitmapImage(); using (WebClient client = new WebClient()) { var data = client.DownloadData(final); using(var stream = new MemoryStream(data)) { img.BeginInit(); img.StreamSource = stream; img.CacheOption = BitmapCacheOption.OnLoad; img.EndInit(); img.Freeze(); } } return img; }) .ContinueWith(parent => { Source = parent.Result; }, TaskScheduler.FromCurrentSynchronizationContext()); }
public Image getImage() { Image img = new Image(); if (!imageCache.ContainsKey(Item.IconURL)) { using (var stream = ApplicationState.Model.GetImage(Item)) { var bitmap = new BitmapImage(); bitmap.BeginInit(); bitmap.StreamSource = stream; bitmap.CacheOption = BitmapCacheOption.OnLoad; bitmap.EndInit(); bitmap.Freeze(); imageCache.Add(Item.IconURL, bitmap); } } img.Source = imageCache[Item.IconURL]; var itemhover = new ItemHover() { DataContext = ItemHoverViewModelFactory.Create(Item) }; Popup popup = new Popup(); popup.AllowsTransparency = true; popup.PopupAnimation = PopupAnimation.Fade; popup.StaysOpen = true; popup.Child = itemhover; popup.PlacementTarget = img; img.Stretch = Stretch.None; img.MouseEnter += (o, e) => { popup.IsOpen = true; }; img.MouseLeave += (o, e) => { popup.IsOpen = false; }; return img; }
/// <summary> /// 将bitmap转换成bitmapImage类型 /// </summary> /// <param name="bitmap"></param> /// <returns>绑定给控件显示的类型</returns> public static BitmapImage GetBmpFromBitmap(Bitmap bitmap) { BitmapImage bitImg = new BitmapImage(); using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) { bitmap.Save(ms, ImageFormat.Bmp); try { bitImg.BeginInit(); bitImg.CacheOption = BitmapCacheOption.OnLoad; bitImg.StreamSource = ms; bitImg.EndInit(); bitImg.Freeze(); return bitImg; } catch (System.Exception ex) { throw new Exception(ex.Message); } } }
/// <summary> /// Returns a 'default' image for when album art wasn't found in a song /// </summary> /// <returns></returns> private static BitmapImage GetDefaultAlbumArt() { var img = new BitmapImage(new Uri("pack://application:,,,/Resources/Images/DefaultAlbumCover.png", UriKind.RelativeOrAbsolute)); img.Freeze(); return img; }
public static BitmapImage GenerateImage_FileStream(string filepath) { BitmapImage bitmapImage = new BitmapImage(); try { using (FileStream fs = new FileStream(filepath, FileMode.Open)) { bitmapImage.BeginInit(); bitmapImage.CacheOption = BitmapCacheOption.OnLoad; bitmapImage.StreamSource = fs; bitmapImage.EndInit(); bitmapImage.Freeze(); } } catch (IOException ioex) { throw new ApplicationException(ioex.Message); } catch (Exception ex) { throw new ApplicationException(ex.Message); } return bitmapImage; }
public static BitmapImage GetImage(string resource, int size = -1, Assembly assembly = null) { BitmapImage bitmap; if (assembly == null) { assembly = Assembly.GetExecutingAssembly(); } using (var stream = assembly.GetManifestResourceStream(resource)) { if (stream != null) { bitmap = new BitmapImage(); bitmap.BeginInit(); bitmap.StreamSource = stream; bitmap.CacheOption = BitmapCacheOption.OnLoad; if (size != -1) { bitmap.DecodePixelHeight = size; bitmap.DecodePixelWidth = size; } bitmap.EndInit(); bitmap.Freeze(); return bitmap; } } return null; }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { var bytes = value as byte[]; if (bytes == null || bytes.Length == 0) return DependencyProperty.UnsetValue; var image = new BitmapImage(); using (Stream stream = new MemoryStream(bytes)) { image.BeginInit(); image.StreamSource = stream; image.CacheOption = BitmapCacheOption.OnLoad; image.EndInit(); } if (!image.IsFrozen) { image.AddMemoryPressure(); image.Freeze(); } return image; }
public static BitmapImage LoadBitmap(string fileName) { #if DEBUG var file = new System.IO.FileInfo(fileName); if (!file.Exists) { Console.WriteLine(file); } #endif try { if (File.Exists(fileName)) { var fileInfo = new System.IO.FileInfo(fileName); var bitmapImage = new BitmapImage(new Uri(fileInfo.FullName)); bitmapImage.Freeze(); var result = bitmapImage; return result; } } catch (ArgumentException) { } catch (IOException) { } return null; }
public static BitmapImage ComposerToThumbnail(Composer composer) { var thumbnail = (BitmapImage)null; if (_thumbnailCache.TryGetValue(composer.ComposerId, out thumbnail)) { return _thumbnailCache[composer.ComposerId]; } var directoryPath = $@"{Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData, Environment.SpecialFolderOption.Create)}\Music Timeline\Resources\Thumbnails\"; if (!Directory.Exists(directoryPath)) { Directory.CreateDirectory(directoryPath); } var thumbnailPath = $@"{Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData, Environment.SpecialFolderOption.Create)}\Music Timeline\Resources\Thumbnails\{composer.ComposerId}.jpg"; var thumbnailUri = new Uri(thumbnailPath, UriKind.Absolute); if (File.Exists(thumbnailPath)) { thumbnail = new BitmapImage(); thumbnail.BeginInit(); thumbnail.DecodePixelHeight = 50; thumbnail.StreamSource = new MemoryStream(File.ReadAllBytes(thumbnailPath)); thumbnail.EndInit(); thumbnail.Freeze(); _thumbnailCache[composer.ComposerId] = thumbnail; return thumbnail; } return CreateThumbnail(composer); }
public BitmapImage Convert(BitmapSource bitmap) { var encoder = new PngBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(bitmap)); var memoryStream = new MemoryStream(); encoder.Save(memoryStream); var bitmapImage = new BitmapImage(); bitmapImage.CacheOption = BitmapCacheOption.OnLoad; bitmapImage.CreateOptions = BitmapCreateOptions.None; try { bitmapImage.BeginInit(); bitmapImage.StreamSource = new MemoryStream(memoryStream.ToArray()); } finally { bitmapImage.EndInit(); bitmapImage.Freeze(); memoryStream.Close(); } return bitmapImage; }
public BitmapImage BitmapImage(string url) { Stream stream = null; WebClient webClient = new WebClient(); BitmapImage image = new BitmapImage(); try { stream = new MemoryStream(webClient.DownloadData(url)); image.BeginInit(); image.CacheOption = BitmapCacheOption.OnLoad; // here image.StreamSource = stream; image.EndInit(); image.Freeze(); } catch { } if (stream != null) { stream.Close(); stream.Dispose(); stream = null; } url = null; webClient.Dispose(); webClient = null; return image; }
internal static async Task LoadImage(this Canvas canvas, WindowData data, int imageIndex) { var image = await Task.Run(delegate { var im = new BitmapImage(new Uri(data.FileList[imageIndex].FileName)); im.Freeze(); return im; }); data.ScaleX = image.Width / image.PixelWidth; data.ScaleY = image.Height / image.PixelHeight; //if (image.PixelWidth != FileAccess.imageWidth || image.PixelHeight != FileAccess.imageHeight) throw new Exception(); var bg = new ImageBrush { ImageSource = image }; data.WindowTitle = Path.GetFileName(data.FileList[imageIndex].FileName) + " (" + bg.ImageSource.Width + "x" + bg.ImageSource.Height + ")"; canvas.Background = bg; canvas.Width = bg.ImageSource.Width; canvas.Height = bg.ImageSource.Height; canvas.Scale(data.Zoom); Keyboard.Focus(canvas); }
public FigureWrapper(byte figure) { InitializeComponent(); FigurePlacedOrChosen = false; Figure = figure; Ellipse e1 = (Ellipse)this.FindName("Ellipse1"); Ellipse e2 = (Ellipse)this.FindName("Ellipse2"); Ellipse e3 = (Ellipse)this.FindName("Ellipse3"); Ellipse e4 = (Ellipse)this.FindName("Ellipse4"); e1.Fill = new SolidColorBrush(((figure & 1) == 0) ? Color.FromArgb(255, 0x44, 0x44, 0x44) : Color.FromArgb(255, 255, 255, 255)); e2.Fill = new SolidColorBrush(((figure & 2) == 0) ? Color.FromArgb(255, 0x44, 0x44, 0x44) : Color.FromArgb(255, 255, 255, 255)); e3.Fill = new SolidColorBrush(((figure & 4) == 0) ? Color.FromArgb(255, 0x44, 0x44, 0x44) : Color.FromArgb(255, 255, 255, 255)); e4.Fill = new SolidColorBrush(((figure & 8) == 0) ? Color.FromArgb(255, 0x44, 0x44, 0x44) : Color.FromArgb(255, 255, 255, 255)); using (Stream _stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Quarto.Images.figure" + figure + ".png")) { BitmapImage bi = new BitmapImage(); bi.BeginInit(); bi.StreamSource = _stream; bi.CacheOption = BitmapCacheOption.OnLoad; bi.EndInit(); bi.Freeze(); this.Background = new ImageBrush { ImageSource = bi }; } SwitchToPieceView(); }
public BitmapImage GetCover(Guid guid, string artist, string album = null) { var item = new byte[] { }; try { item = Get(guid); } catch (DataIdentifierNotFoundException) { if (SettingsModel.Instance.CoverDownloadEnabled) { item = DownloadImage(guid, artist, album, 5); } } if (item.Length == 0) return _defaultCover; var source = new BitmapImage(); using (Stream stream = new MemoryStream(item)) { source.BeginInit(); source.StreamSource = stream; source.CacheOption = BitmapCacheOption.OnLoad; source.EndInit(); } source.Freeze(); return source; }
private static BitmapImage ByteArrayToBitmap(object bytesArray) { try { if (bytesArray == null || bytesArray.GetType() != typeof(Byte[])) return null; var binaryData = (byte[])bytesArray; var bmp = new BitmapImage(); using (var stream = new MemoryStream(binaryData)) { bmp.BeginInit(); bmp.StreamSource = stream; bmp.CacheOption = BitmapCacheOption.OnLoad; bmp.EndInit(); } if (bmp.CanFreeze) bmp.Freeze(); return bmp; } catch (Exception e) { return null; } }
public MulticastMessageEditWindow(Tag tag, string comment, AmoebaManager amoebaManager) { _tag = tag; _amoebaManager = amoebaManager; _digitalSignatureCollection.AddRange(Settings.Instance.Global_DigitalSignatures.ToArray()); InitializeComponent(); this.Title = LanguagesManager.Instance.MulticastMessageEditWindow_Title + " - " + MessageConverter.ToTagString(tag); { var icon = new BitmapImage(); icon.BeginInit(); icon.StreamSource = new FileStream(Path.Combine(_serviceManager.Paths["Icons"], "Amoeba.ico"), FileMode.Open, FileAccess.Read, FileShare.Read); icon.EndInit(); if (icon.CanFreeze) icon.Freeze(); this.Icon = icon; } _signatureComboBox.ItemsSource = _digitalSignatureCollection; _signatureComboBox.SelectedIndex = 0; _commentTextBox.Text = comment; _watchTimer = new WatchTimer(this.WatchThread, 0, 1000); this.Closed += (sender, e) => _watchTimer.Dispose(); _textEditor_Helper.Setup(_textEditor); }
public static BitmapImage GetAlbumArt(string path) { if(path != null) { try { TagLib.File tagFile = TagLib.File.Create(path); if (tagFile.Tag.Pictures.Length != 0) { MemoryStream ms = new MemoryStream(tagFile.Tag.Pictures[0].Data.Data); ms.Seek(0, SeekOrigin.Begin); // ImageSource for System.Windows.Controls.Image BitmapImage bitmap = new BitmapImage(); bitmap.BeginInit(); bitmap.StreamSource = ms; bitmap.EndInit(); bitmap.Freeze(); return bitmap; } } catch(Exception e) { } } return null; }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { byte[] data = value as byte[]; if (data != null) { BitmapImage bm = new BitmapImage(); try { using (System.IO.MemoryStream ms = new System.IO.MemoryStream(data)) { bm.BeginInit(); bm.CreateOptions = BitmapCreateOptions.PreservePixelFormat; bm.CacheOption = BitmapCacheOption.OnLoad; bm.UriSource = null; bm.StreamSource = ms; bm.EndInit(); } bm.Freeze(); } catch { bm = null; } return bm; } return null; }
public static System.Windows.Media.Imaging.BitmapSource ToWpfBitmap(this Bitmap bitmap) { using (MemoryStream stream = new MemoryStream()) { bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp); stream.Position = 0; System.Windows.Media.Imaging.BitmapImage result = new System.Windows.Media.Imaging.BitmapImage(); result.BeginInit(); result.CacheOption = System.Windows.Media.Imaging.BitmapCacheOption.OnLoad; result.StreamSource = stream; result.EndInit(); result.Freeze(); return(result); } }
public static System.Windows.Media.Imaging.BitmapImage ToImage(byte[] array) { using (var ms = new System.IO.MemoryStream(array)) { System.Windows.Media.Imaging.BitmapImage image = null; System.Windows.Application.Current.Dispatcher.Invoke((Action)(() => { image = new System.Windows.Media.Imaging.BitmapImage(); image.BeginInit(); image.CacheOption = System.Windows.Media.Imaging.BitmapCacheOption.OnLoad; image.StreamSource = ms; image.EndInit(); image.Freeze(); })); return(image); } }
// Loads image on memory (to make it deletable on another view) public static bool LoadPictureInto(System.Windows.Controls.Image image, string path) { var loaded = false; if ((image is System.Windows.Controls.Image) && path != null && !path.Equals(string.Empty) && File.Exists(path)) { var source = new System.Windows.Media.Imaging.BitmapImage(); source.BeginInit(); source.CacheOption = BitmapCacheOption.OnLoad; source.CreateOptions = BitmapCreateOptions.IgnoreImageCache; source.UriSource = new Uri(path, UriKind.Absolute); source.EndInit(); image.Source = source; source.Freeze(); loaded = true; } return(loaded); }
/// <summary> /// Wandelt ein WinForms (GDI) Image in ein WPF Image um. /// </summary> /// <param name="img"></param> /// <returns></returns> public static ImageSource ConvertDrawingImageToWPFImage(System.Drawing.Image img) { MemoryStream ms = new MemoryStream(); img.Save(ms, System.Drawing.Imaging.ImageFormat.Png); System.Windows.Media.Imaging.BitmapImage bImg = new System.Windows.Media.Imaging.BitmapImage(); bImg.BeginInit(); bImg.StreamSource = new MemoryStream(ms.ToArray()); bImg.EndInit(); bImg.Freeze(); return(bImg); }
private void m_Camera_OnFrameCaptrue(Bitmap bitmap) { System.Windows.Media.Imaging.BitmapImage m_Frame = new System.Windows.Media.Imaging.BitmapImage(); using (var stream = new System.IO.MemoryStream()) { bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp); stream.Seek(0, System.IO.SeekOrigin.Begin); m_Frame.BeginInit(); m_Frame.CacheOption = System.Windows.Media.Imaging.BitmapCacheOption.OnLoad; m_Frame.StreamSource = stream; m_Frame.EndInit(); stream.SetLength(0); stream.Capacity = 0; stream.Dispose(); } m_Frame.Freeze(); m_ControlWindow.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.SystemIdle, m_Refresh, m_Frame); }
public static System.Windows.Media.Imaging.BitmapImage ToImageUnsafe(byte[] bytes) { System.Windows.Media.Imaging.BitmapImage image = null; using (Stream ms = new System.IO.MemoryStream(bytes)) { System.Windows.Application.Current.Dispatcher.Invoke((Action)(() => { sw.Reset(); sw.Start(); image = new System.Windows.Media.Imaging.BitmapImage(); image.BeginInit(); image.CacheOption = System.Windows.Media.Imaging.BitmapCacheOption.OnLoad; image.StreamSource = ms; image.EndInit(); image.Freeze(); })); } return(image); }
private void newAforgeFrameHandler(object sender, AForge.Video.NewFrameEventArgs e) { try { System.Drawing.Image newRawImage = (System.Drawing.Bitmap)e.Frame.Clone(); newRawImage.Save(_newFrameMS, System.Drawing.Imaging.ImageFormat.Bmp); _newFrameMS.Seek(0, System.IO.SeekOrigin.Begin); System.Windows.Media.Imaging.BitmapImage newFrameImage = new System.Windows.Media.Imaging.BitmapImage(); newFrameImage.BeginInit(); newFrameImage.StreamSource = _newFrameMS; newFrameImage.EndInit(); newFrameImage.Freeze(); this.AforgeFrameImage = newFrameImage; } catch { } }
public static WImage.BitmapImage BitmapToBitmapImage(Draw.Bitmap bitmap) { //var tempFile= Path.GetTempFileName(); // bitmap.Save(tempFile, ImageFormat.Jpeg); // BitmapImage image = null; // Application.Current.Dispatcher.Invoke(() => image = new BitmapImage(new Uri(tempFile))); // return image; using (var memory = new MemoryStream()) { bitmap.Save(memory, Draw.Imaging.ImageFormat.Jpeg); memory.Position = 0; var bitmapImage = new WImage.BitmapImage(); bitmapImage.BeginInit(); bitmapImage.StreamSource = memory; bitmapImage.CacheOption = WImage.BitmapCacheOption.OnLoad; bitmapImage.EndInit(); bitmapImage.Freeze(); return(bitmapImage); } }
public static ImageSource ToImageSource(this string filePath) { using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); fs.Close(); fs.Dispose(); System.Windows.Media.Imaging.BitmapImage bitmapImage = new System.Windows.Media.Imaging.BitmapImage(); using (System.IO.MemoryStream ms = new System.IO.MemoryStream(buffer)) { bitmapImage.BeginInit(); bitmapImage.StreamSource = ms; bitmapImage.CacheOption = System.Windows.Media.Imaging.BitmapCacheOption.OnLoad; bitmapImage.EndInit(); bitmapImage.Freeze(); } return(bitmapImage); } }
public static BitmapImage ToBitmapImage(this System.Drawing.Image aImage) { try { using (MemoryStream ms = new MemoryStream()) { aImage.Save(ms, aImage.RawFormat); System.Windows.Media.Imaging.BitmapImage bImg = new System.Windows.Media.Imaging.BitmapImage(); bImg.BeginInit(); bImg.StreamSource = new MemoryStream(ms.ToArray()); bImg.EndInit(); bImg.Freeze(); return(bImg); } } catch (Exception e) { Trace.WriteLine(Trace.kKinskyDesktop, "Error loading image: " + e.Message); Trace.WriteLine(Trace.kKinskyDesktop, e.StackTrace); return(null); } }
protected override BitmapImage DownloadBitmap(Uri uri) { BeginDownload(); BitmapImage result = null; try { result = _mapsHandler.GetTileBitmap(uri); if (result != null) { #if !DEBUG SaveCacheImage(result, uri); #endif } else { lock (this) { result = new System.Windows.Media.Imaging.BitmapImage(); result.BeginInit(); result.CacheOption = System.Windows.Media.Imaging.BitmapCacheOption.OnLoad; result.StreamSource = Assembly.GetExecutingAssembly().GetManifestResourceStream("GlobalcachingApplication.Plugins.Maps.OSMBinMap.MissingTile.png"); result.EndInit(); result.Freeze(); } } } catch { result = null; RaiseDownloadError(); } finally { EndDownload(); } return(result); }
protected override BitmapImage DownloadBitmap(Uri uri) { BeginDownload(); BitmapImage result = null; try { result = _mapsHandler.GetTileBitmap(uri); if (result != null) { #if !DEBUG SaveCacheImage(result, uri); #endif } else { lock (this) { result = new System.Windows.Media.Imaging.BitmapImage(); result.BeginInit(); result.CacheOption = System.Windows.Media.Imaging.BitmapCacheOption.OnLoad; result.UriSource = Utils.ResourceHelper.GetResourceUri("/MapProviders/OSMBinMap/MissingTile.png"); result.EndInit(); result.Freeze(); } } } catch { result = null; RaiseDownloadError(); } finally { EndDownload(); } return(result); }