GetImageSynchronousIgnoreDpi ( String uriString ) { Debug.Assert(!String.IsNullOrEmpty(uriString)); AssertValid(); BitmapSource oBitmapSource = GetImageSynchronous(uriString); if (oBitmapSource.DpiX != m_dScreenDpi) { // The DPI properties of BitmapSource are read-only. To work // around this, copy the BitmapSource to a new BitmapSource, // changing the DPI in the process. Wasteful, but there doesn't // seem to be a better way. // The formula for stride, which isn't documented in MSDN, was // taken from here: // // http://social.msdn.microsoft.com/Forums/en/windowswic/thread/ // 8e2d2dbe-6819-488b-ac49-bf5235d87bc4 Int32 iStride = ((oBitmapSource.PixelWidth * oBitmapSource.Format.BitsPerPixel) + 7) / 8; // Also, see this: // // http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/ // 56364b28-1277-4be8-8d45-78788cc4f2d7/ Byte [] abtPixels = new Byte[oBitmapSource.PixelHeight * iStride]; oBitmapSource.CopyPixels(abtPixels, iStride, 0); oBitmapSource = BitmapSource.Create(oBitmapSource.PixelWidth, oBitmapSource.PixelHeight, m_dScreenDpi, m_dScreenDpi, oBitmapSource.Format, oBitmapSource.Palette, abtPixels, iStride ); WpfGraphicsUtil.FreezeIfFreezable(oBitmapSource); } return(oBitmapSource); }
GetImageSynchronousHttp ( Uri uri ) { Debug.Assert(uri != null); Debug.Assert(uri.Scheme == Uri.UriSchemeHttp || uri.Scheme == Uri.UriSchemeHttps); AssertValid(); // Talk about inefficient... // // The following code uses HttpWebRequest to synchronously download the // image into a List<Byte>, then passes that List as a MemoryStream to // BitmapImage.StreamSource. It works, but it involves way too much // Byte copying. There has to be a better way to do this, but so far I // haven't found one. // // In the following post... // // http://stackoverflow.com/questions/426645/ // how-to-render-an-image-in-a-background-wpf-process // // ...the poster suggests feeding the WebResponse.GetResponseStream() // indirectly to BitmapImage.StreamSource. This sometimes works but // sometimes doesn't, which you can tell by checking // BitmapImage.IsDownloading at the end of his code. It is true // sometimes, indicating that the Bitmap is still downloading. HttpWebRequest oHttpWebRequest = (HttpWebRequest)WebRequest.Create(uri); oHttpWebRequest.Timeout = HttpWebRequestTimeoutMs; oHttpWebRequest.CachePolicy = new RequestCachePolicy( RequestCacheLevel.CacheIfAvailable); WebResponse oWebResponse = oHttpWebRequest.GetResponse(); BinaryReader oBinaryReader = new BinaryReader( oWebResponse.GetResponseStream()); List <Byte> oResponseBytes = new List <Byte>(); while (true) { Byte [] abtSomeResponseBytes = oBinaryReader.ReadBytes(8192); if (abtSomeResponseBytes.Length == 0) { break; } oResponseBytes.AddRange(abtSomeResponseBytes); } Byte [] abtAllResponseBytes = oResponseBytes.ToArray(); oResponseBytes = null; MemoryStream oMemoryStream = new MemoryStream(abtAllResponseBytes, false); BitmapImage oBitmapImage = new BitmapImage(); oBitmapImage.BeginInit(); oBitmapImage.StreamSource = oMemoryStream; oBitmapImage.EndInit(); WpfGraphicsUtil.FreezeIfFreezable(oBitmapImage); return(oBitmapImage); }
GetImageSynchronous ( String uriString ) { Debug.Assert(!String.IsNullOrEmpty(uriString)); AssertValid(); const Int32 ErrorImageWidthAndHeight = 52; Uri oUri; if (Uri.TryCreate(uriString, UriKind.Absolute, out oUri)) { if (oUri.Scheme == Uri.UriSchemeHttp || oUri.Scheme == Uri.UriSchemeHttps) { try { return(GetImageSynchronousHttp(oUri)); } catch (WebException) { // (These empty catch blocks cause an error image to be // returned.) } catch (ArgumentException) { /* * Attempting to download a possibly corrupt image from this * Twitter URL: * * http://a1.twimg.com/profile_images/1112245377/ * Photo_on_2010-08-27_at_18.51_normal.jpg * * raised the following exception: * * [ArgumentException]: An invalid character was found in text * content. * at System.Windows.Media.Imaging.BitmapFrameDecode. * get_ColorContexts() * at System.Windows.Media.Imaging.BitmapImage. * FinalizeCreation() * at System.Windows.Media.Imaging.BitmapImage.EndInit() */ } catch (IOException) { /* * Attempting to download a possibly corrupt image from this * Twitter URL: * * http://a2.twimg.com/profile_images/1126755034/ * 8FjbVD_normal.gif * * raised the following exception: * * [IOException]: Cannot read from the stream. * [COMException]: Exception from HRESULT: 0x88982F72 * at System.Windows.Media.Imaging.BitmapDecoder. * SetupDecoderFromUriOrStream(Uri uri, Stream stream, * BitmapCacheOption cacheOption, Guid& clsId, Boolean& * isOriginalWritable, Stream& uriStream, * UnmanagedMemoryStream& unmanagedMemoryStream, * SafeFileHandle& safeFilehandle) * ... */ } catch (NotSupportedException) { /* * Attempting to download a possibly corrupt image from a * Twitter URL (didn't record the URL) raised the following * exception: * * [NotSupportedException]: No imaging component suitable * to complete this operation was found. */ } } else if (oUri.Scheme == Uri.UriSchemeFile) { try { BitmapImage oBitmapImage = new BitmapImage(oUri); WpfGraphicsUtil.FreezeIfFreezable(oBitmapImage); return(oBitmapImage); } catch (IOException) { } catch (NotSupportedException) { // Invalid image file. } catch (UnauthorizedAccessException) { // The URI is actually a folder, for example. } } } if (m_oCachedErrorImage == null) { m_oCachedErrorImage = CreateErrorImage(ErrorImageWidthAndHeight); } return(m_oCachedErrorImage); }