/// <summary> /// Gets an instance of an Image. /// </summary> /// <param name="url">an URL</param> /// <returns>an object of type Gif, Jpeg or Png</returns> public static Image GetInstance(Uri url, bool handleIncorrectImage) { Stream istr = null; try { WebRequest w = WebRequest.Create(url); w.Credentials = CredentialCache.DefaultCredentials; istr = w.GetResponse().GetResponseStream(); int c1 = istr.ReadByte(); int c2 = istr.ReadByte(); int c3 = istr.ReadByte(); int c4 = istr.ReadByte(); // jbig2 int c5 = istr.ReadByte(); int c6 = istr.ReadByte(); int c7 = istr.ReadByte(); int c8 = istr.ReadByte(); istr.Close(); istr = null; if (c1 == 'G' && c2 == 'I' && c3 == 'F') { GifImage gif = new GifImage(url); Image img = gif.GetImage(1); return img; } if (c1 == 0xFF && c2 == 0xD8) { return new Jpeg(url); } if (c1 == 0x00 && c2 == 0x00 && c3 == 0x00 && c4 == 0x0c) { return new Jpeg2000(url); } if (c1 == 0xff && c2 == 0x4f && c3 == 0xff && c4 == 0x51) { return new Jpeg2000(url); } if (c1 == PngImage.PNGID[0] && c2 == PngImage.PNGID[1] && c3 == PngImage.PNGID[2] && c4 == PngImage.PNGID[3]) { Image img = PngImage.GetImage(url); return img; } if (c1 == 0xD7 && c2 == 0xCD) { Image img = new ImgWMF(url); return img; } if (c1 == 'B' && c2 == 'M') { Image img = BmpImage.GetImage(url); return img; } if ((c1 == 'M' && c2 == 'M' && c3 == 0 && c4 == 42) || (c1 == 'I' && c2 == 'I' && c3 == 42 && c4 == 0)) { RandomAccessFileOrArray ra = null; try { if (url.IsFile) { String file = url.LocalPath; ra = new RandomAccessFileOrArray(file); } else ra = new RandomAccessFileOrArray(url); Image img = TiffImage.GetTiffImage(ra, handleIncorrectImage, 1); img.url = url; return img; } finally { if (ra != null) ra.Close(); } } if ( c1 == 0x97 && c2 == 'J' && c3 == 'B' && c4 == '2' && c5 == '\r' && c6 == '\n' && c7 == 0x1a && c8 == '\n' ) { RandomAccessFileOrArray ra = null; try { if (url.IsFile) { String file = url.LocalPath; ra = new RandomAccessFileOrArray(file); } else ra = new RandomAccessFileOrArray(url); Image img = JBIG2Image.GetJbig2Image(ra, 1); img.url = url; return img; } finally { if (ra != null) ra.Close(); } } throw new IOException(MessageLocalization.GetComposedMessage("unknown.image.format", url.ToString())); } finally { if (istr != null) { istr.Close(); } } }
/// <summary> /// Gets an instance of an Image. /// </summary> /// <param name="url">an URL</param> /// <returns>an object of type Gif, Jpeg or Png</returns> public static Image GetInstance(Uri url) { Stream istr = null; try { //NETCF does not know fileWebRequest (file://...) #if !NETCF WebRequest w = WebRequest.Create(url); istr = w.GetResponse().GetResponseStream(); #else istr = new FileStream(url.LocalPath, FileMode.Open); #endif int c1 = istr.ReadByte(); int c2 = istr.ReadByte(); int c3 = istr.ReadByte(); int c4 = istr.ReadByte(); istr.Close(); istr = null; if (c1 == 'G' && c2 == 'I' && c3 == 'F') { GifImage gif = new GifImage(url); Image img = gif.GetImage(1); return img; } if (c1 == 0xFF && c2 == 0xD8) { return new Jpeg(url); } if (c1 == PngImage.PNGID[0] && c2 == PngImage.PNGID[1] && c3 == PngImage.PNGID[2] && c4 == PngImage.PNGID[3]) { Image img = PngImage.GetImage(url); return img; } if (c1 == 0xD7 && c2 == 0xCD) { Image img = new ImgWMF(url); return img; } if (c1 == 'B' && c2 == 'M') { Image img = BmpImage.GetImage(url); return img; } if ((c1 == 'M' && c2 == 'M' && c3 == 0 && c4 == 42) || (c1 == 'I' && c2 == 'I' && c3 == 42 && c4 == 0)) { RandomAccessFileOrArray ra = null; try { if (url.IsFile) { String file = url.LocalPath; ra = new RandomAccessFileOrArray(file); } else ra = new RandomAccessFileOrArray(url); Image img = TiffImage.GetTiffImage(ra, 1); img.url = url; return img; } catch (InvalidCastException ix) { System.Diagnostics.Debug.WriteLine("InvalidCastException in TIFF adding: " + ix.Message + ":" + ix.StackTrace); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Exception in TIFF adding:" + ex.Message + ":" + ex.InnerException.Message); } finally { if (ra != null) ra.Close(); } } throw new IOException(url.ToString() + " is not a recognized imageformat."); } finally { if (istr != null) { istr.Close(); } } }
/// <summary> /// Gets an instance of an Image. /// </summary> /// <param name="url">an URL</param> /// <returns>an object of type Gif, Jpeg or Png</returns> public static Image GetInstance(Uri url, bool recoverFromImageError) { Stream istr = null; RandomAccessSourceFactory randomAccessSourceFactory = new RandomAccessSourceFactory(); try { WebRequest w = WebRequest.Create(url); w.Credentials = CredentialCache.DefaultCredentials; istr = w.GetResponse().GetResponseStream(); int c1 = istr.ReadByte(); int c2 = istr.ReadByte(); int c3 = istr.ReadByte(); int c4 = istr.ReadByte(); // jbig2 int c5 = istr.ReadByte(); int c6 = istr.ReadByte(); int c7 = istr.ReadByte(); int c8 = istr.ReadByte(); istr.Close(); istr = null; if (c1 == 'G' && c2 == 'I' && c3 == 'F') { GifImage gif = new GifImage(url); Image img = gif.GetImage(1); return img; } if (c1 == 0xFF && c2 == 0xD8) { return new Jpeg(url); } if (c1 == 0x00 && c2 == 0x00 && c3 == 0x00 && c4 == 0x0c) { return new Jpeg2000(url); } if (c1 == 0xff && c2 == 0x4f && c3 == 0xff && c4 == 0x51) { return new Jpeg2000(url); } if (c1 == PngImage.PNGID[0] && c2 == PngImage.PNGID[1] && c3 == PngImage.PNGID[2] && c4 == PngImage.PNGID[3]) { Image img = PngImage.GetImage(url); return img; } if (c1 == 0xD7 && c2 == 0xCD) { Image img = new ImgWMF(url); return img; } if (c1 == 'B' && c2 == 'M') { Image img = BmpImage.GetImage(url); return img; } if ((c1 == 'M' && c2 == 'M' && c3 == 0 && c4 == 42) || (c1 == 'I' && c2 == 'I' && c3 == 42 && c4 == 0)) { RandomAccessFileOrArray ra = null; try { if (url.IsFile) { String file = url.LocalPath; ra = new RandomAccessFileOrArray(randomAccessSourceFactory.CreateBestSource(file)); } else ra = new RandomAccessFileOrArray(randomAccessSourceFactory.CreateSource(url)); Image img = TiffImage.GetTiffImage(ra, 1); img.url = url; return img; } catch (Exception e) { if (recoverFromImageError) { // reruns the getTiffImage() with several error recovering workarounds in place // not guaranteed to work with every TIFF Image img = TiffImage.GetTiffImage(ra, recoverFromImageError, 1); img.url = url; return img; } throw e; } finally { if (ra != null) ra.Close(); } } if ( c1 == 0x97 && c2 == 'J' && c3 == 'B' && c4 == '2' && c5 == '\r' && c6 == '\n' && c7 == 0x1a && c8 == '\n' ) { RandomAccessFileOrArray ra = null; try { if (url.IsFile) { String file = url.LocalPath; ra = new RandomAccessFileOrArray(randomAccessSourceFactory.CreateBestSource(file)); } else ra = new RandomAccessFileOrArray(randomAccessSourceFactory.CreateSource(url)); Image img = JBIG2Image.GetJbig2Image(ra, 1); img.url = url; return img; } finally { if (ra != null) ra.Close(); } } throw new IOException(MessageLocalization.GetComposedMessage("unknown.image.format", url.ToString())); } finally { if (istr != null) { istr.Close(); } } }
/// <summary> /// Gets an instance of an Image. /// </summary> /// <param name="url">an URL</param> /// <returns>an object of type Gif, Jpeg or Png</returns> public static Image GetLocalInstance(string sFileName) { Stream istr = null; try { #if !NETCF WebRequest w = WebRequest.Create(url); istr = w.GetResponse().GetResponseStream(); #else istr = new FileStream(sFileName, FileMode.Open); #endif //istr = w.GetResponse().GetResponseStream(); int c1 = istr.ReadByte(); int c2 = istr.ReadByte(); int c3 = istr.ReadByte(); int c4 = istr.ReadByte(); istr.Close(); istr = null; if (c1 == 'G' && c2 == 'I' && c3 == 'F') { GifImage gif = new GifImage(sFileName); Image img = gif.GetImage(1); return img; } if (c1 == 0xFF && c2 == 0xD8) { return new Jpeg(sFileName); } if (c1 == PngImage.PNGID[0] && c2 == PngImage.PNGID[1] && c3 == PngImage.PNGID[2] && c4 == PngImage.PNGID[3]) { Image img = PngImage.GetImage(sFileName); return img; } if (c1 == 0xD7 && c2 == 0xCD) { Image img = new ImgWMF(sFileName); return img; } if (c1 == 'B' && c2 == 'M') { Image img = BmpImage.GetImage(sFileName); return img; } if ((c1 == 'M' && c2 == 'M' && c3 == 0 && c4 == 42) || (c1 == 'I' && c2 == 'I' && c3 == 42 && c4 == 0)) { RandomAccessFileOrArray ra = null; try { ra = new RandomAccessFileOrArray(sFileName); Image img = TiffImage.GetTiffImage(ra, 1); img.url = new Uri(sFileName); return img; } finally { if (ra != null) ra.Close(); } } throw new IOException(sFileName.ToString() + " is not a recognized imageformat."); } finally { if (istr != null) { istr.Close(); } } }
/// <summary> /// Gets an instance of an Image. /// </summary> /// <param name="url">an URL</param> /// <returns>an object of type Gif, Jpeg or Png</returns> public static Image GetInstance(Uri url) { // Add support for base64 encoded pngs. if (url.Scheme == "data") { if (url.AbsoluteUri.StartsWith("data:image/png;base64,")) { var base64Image = url.AbsoluteUri.Substring("data:image/png;base64,".Length); var image = Convert.FromBase64String(base64Image); return PngImage.GetImage(image); } } Stream istr = null; try { WebRequest w = WebRequest.Create(url); istr = w.GetResponse().GetResponseStream(); int c1 = istr.ReadByte(); int c2 = istr.ReadByte(); int c3 = istr.ReadByte(); int c4 = istr.ReadByte(); // jbig2 int c5 = istr.ReadByte(); int c6 = istr.ReadByte(); int c7 = istr.ReadByte(); int c8 = istr.ReadByte(); istr.Close(); istr = null; if (c1 == 'G' && c2 == 'I' && c3 == 'F') { GifImage gif = new GifImage(url); Image img = gif.GetImage(1); return img; } if (c1 == 0xFF && c2 == 0xD8) { return new Jpeg(url); } if (c1 == 0x00 && c2 == 0x00 && c3 == 0x00 && c4 == 0x0c) { return new Jpeg2000(url); } if (c1 == 0xff && c2 == 0x4f && c3 == 0xff && c4 == 0x51) { return new Jpeg2000(url); } if (c1 == PngImage.PNGID[0] && c2 == PngImage.PNGID[1] && c3 == PngImage.PNGID[2] && c4 == PngImage.PNGID[3]) { Image img = PngImage.GetImage(url); return img; } if (c1 == 0xD7 && c2 == 0xCD) { Image img = new ImgWMF(url); return img; } if (c1 == 'B' && c2 == 'M') { Image img = BmpImage.GetImage(url); return img; } if ((c1 == 'M' && c2 == 'M' && c3 == 0 && c4 == 42) || (c1 == 'I' && c2 == 'I' && c3 == 42 && c4 == 0)) { RandomAccessFileOrArray ra = null; try { if (url.IsFile) { String file = url.LocalPath; ra = new RandomAccessFileOrArray(file); } else ra = new RandomAccessFileOrArray(url); Image img = TiffImage.GetTiffImage(ra, 1); img.url = url; return img; } finally { if (ra != null) ra.Close(); } } if ( c1 == 0x97 && c2 == 'J' && c3 == 'B' && c4 == '2' && c5 == '\r' && c6 == '\n' && c7 == 0x1a && c8 == '\n' ) { RandomAccessFileOrArray ra = null; try { if (url.IsFile) { String file = url.LocalPath; ra = new RandomAccessFileOrArray(file); } else ra = new RandomAccessFileOrArray(url); Image img = JBIG2Image.GetJbig2Image(ra, 1); img.url = url; return img; } finally { if (ra != null) ra.Close(); } } throw new IOException(url.ToString() + " is not a recognized imageformat."); } finally { if (istr != null) { istr.Close(); } } }