private void TraceCodecInfo(BitmapCodecInfo codec) { Console.WriteLine("friendlyname: " + codec.FriendlyName); Console.WriteLine("extensions: " + codec.FileExtensions); Console.WriteLine("version: " + codec.Version); Console.WriteLine("mimes: " + codec.MimeTypes); Console.WriteLine("author: " + codec.Author); }
GetEncoder( BitmapSource bitmapSource ) { BitmapEncoder encoder = null; if (bitmapSource is BitmapFrame) { // // This code gets the encoder based on the decoder that was // used for this specific BitmapSource. // BitmapFrame bitmapImage = bitmapSource as BitmapFrame; BitmapCodecInfo codecInfo = null; if (bitmapImage != null && bitmapImage.Decoder != null) { codecInfo = bitmapImage.Decoder.CodecInfo; } if (codecInfo != null) { encoder = BitmapEncoder.Create(codecInfo.ContainerFormat); // Avoid GIF encoder which does not save transparency well if (!(encoder is JpegBitmapEncoder || encoder is PngBitmapEncoder || encoder is TiffBitmapEncoder || encoder is WmpBitmapEncoder) ) { encoder = null; } } } // // The code above assumes that the BitmapSource is actually // a BitmapImage. If it is not then we assume Png and use // that encoder. // if (encoder == null) { if (Microsoft.Internal.AlphaFlattener.Utility.NeedPremultiplyAlpha(bitmapSource)) { encoder = new WmpBitmapEncoder(); } else { encoder = new PngBitmapEncoder(); } } return(encoder); }
IsSupportedMimeType( BitmapSource bitmapSource ) { BitmapCodecInfo codecInfo = null; string imageMimeType = ""; if (bitmapSource is BitmapFrame) { // // This code gets the encoder based on the decoder that was // used for this specific BitmapSource. // BitmapFrame bitmapFrame = bitmapSource as BitmapFrame; if (bitmapFrame != null && bitmapFrame.Decoder != null) { codecInfo = bitmapFrame.Decoder.CodecInfo; } } if (codecInfo != null) { imageMimeType = codecInfo.MimeTypes; } int start = 0; int comma = imageMimeType.IndexOf(',', start); bool foundType = false; // // Test all strings before commas // if (comma != -1) { while (comma != -1 && !foundType) { string subString = imageMimeType.Substring(start, comma); foundType = XpsManager.SupportedImageType(new ContentType(subString)); start = comma + 1; comma = imageMimeType.IndexOf(',', start); } } // // If we still have not found a supported type // Test the remainder of the string // if (!foundType) { foundType = XpsManager.SupportedImageType(new ContentType(imageMimeType.Substring(start))); } return(foundType); }
public static IEnumerable <BitmapCodecInfo> GetSupportedCodecs() { foreach (Type type in typeof(BitmapEncoder).Assembly.GetTypes()) { if (type.IsSubclassOf(typeof(BitmapEncoder)) && !type.IsAbstract && type.IsPublic) { if (type.GetConstructor(Type.EmptyTypes) != null) { BitmapCodecInfo info = null; try { BitmapEncoder encoder = (BitmapEncoder)Activator.CreateInstance(type); info = encoder.CodecInfo; } catch (NotSupportedException) { System.Diagnostics.Debug.Fail("Failed to get codec info for image encoder"); continue; } yield return(info); } } } }
private BitmapEncoder GetBitmapEncoder(string fileExtension) { BitmapEncoder bitmapEncoder = null; if (_bitmapEncoder != null && _bitmapEncoder.CodecInfo != null) { string mimeType = string.Empty; BitmapCodecInfo codecInfo = _bitmapEncoder.CodecInfo; string mimeTypes = codecInfo.MimeTypes; string fileExtensions = codecInfo.FileExtensions; switch (_encoderType) { case ImageEncoderType.BmpBitmap: mimeType = "image/bmp"; break; case ImageEncoderType.GifBitmap: mimeType = "image/gif"; break; case ImageEncoderType.JpegBitmap: mimeType = "image/jpeg,image/jpe,image/jpg"; break; case ImageEncoderType.PngBitmap: mimeType = "image/png"; break; case ImageEncoderType.TiffBitmap: mimeType = "image/tiff,image/tif"; break; case ImageEncoderType.WmpBitmap: mimeType = "image/vnd.ms-photo"; break; } if (!string.IsNullOrWhiteSpace(fileExtensions) && fileExtensions.IndexOf(fileExtension, StringComparison.OrdinalIgnoreCase) >= 0) { bitmapEncoder = _bitmapEncoder; } else if (!string.IsNullOrWhiteSpace(mimeTypes) && !string.IsNullOrWhiteSpace(mimeType)) { string[] arrayMimeTypes = mimeType.Split(','); for (int i = 0; i < arrayMimeTypes.Length; i++) { if (mimeTypes.IndexOf(arrayMimeTypes[i], StringComparison.OrdinalIgnoreCase) >= 0) { bitmapEncoder = _bitmapEncoder; break; } } } } if (bitmapEncoder == null) { bitmapEncoder = GetBitmapEncoder(_encoderType); } return(bitmapEncoder); }
private BitmapEncoder GetBitmapEncoder(string fileExtension) { BitmapEncoder bitmapEncoder = null; if (_bitmapEncoder != null && _bitmapEncoder.CodecInfo != null) { string mimeType = String.Empty; BitmapCodecInfo codecInfo = _bitmapEncoder.CodecInfo; string mimeTypes = codecInfo.MimeTypes; string fileExtensions = codecInfo.FileExtensions; switch (_encoderType) { case ImageEncoderType.BmpBitmap: mimeType = "image/bmp"; break; case ImageEncoderType.GifBitmap: mimeType = "image/gif"; break; case ImageEncoderType.JpegBitmap: mimeType = "image/jpeg,image/jpe,image/jpg"; break; case ImageEncoderType.PngBitmap: mimeType = "image/png"; break; case ImageEncoderType.TiffBitmap: mimeType = "image/tiff,image/tif"; break; case ImageEncoderType.WmpBitmap: mimeType = "image/vnd.ms-photo"; break; } if (!String.IsNullOrEmpty(fileExtensions) && fileExtensions.IndexOf(fileExtension, StringComparison.OrdinalIgnoreCase) >= 0) { bitmapEncoder = _bitmapEncoder; } else if (!String.IsNullOrEmpty(mimeTypes) && !String.IsNullOrEmpty(mimeType)) { string[] arrayMimeTypes = mimeType.Split(','); for (int i = 0; i < arrayMimeTypes.Length; i++) { if (mimeTypes.IndexOf(arrayMimeTypes[i], StringComparison.OrdinalIgnoreCase) >= 0) { bitmapEncoder = _bitmapEncoder; break; } } } } if (bitmapEncoder == null) { switch (_encoderType) { case ImageEncoderType.BmpBitmap: bitmapEncoder = new BmpBitmapEncoder(); break; case ImageEncoderType.GifBitmap: bitmapEncoder = new GifBitmapEncoder(); break; case ImageEncoderType.JpegBitmap: JpegBitmapEncoder jpgEncoder = new JpegBitmapEncoder(); // Set the default/user options... bitmapEncoder = jpgEncoder; break; case ImageEncoderType.PngBitmap: PngBitmapEncoder pngEncoder = new PngBitmapEncoder(); // Set the default/user options... bitmapEncoder = pngEncoder; break; case ImageEncoderType.TiffBitmap: bitmapEncoder = new TiffBitmapEncoder(); break; case ImageEncoderType.WmpBitmap: WmpBitmapEncoder wmpEncoder = new WmpBitmapEncoder(); // Set the default/user options... bitmapEncoder = wmpEncoder; break; } } if (bitmapEncoder == null) { bitmapEncoder = new PngBitmapEncoder(); } return(bitmapEncoder); }
ConvertTo( ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType ) { string bitmapName = "bitmap" + m_bitmapId; BitmapEncoder encoder = null; m_bitmapId++; BitmapFrame bmpd = value as BitmapFrame; if (bmpd != null) { BitmapCodecInfo codec = null; if (bmpd.Decoder != null) { codec = bmpd.Decoder.CodecInfo; } if (codec != null) { encoder = BitmapEncoder.Create(codec.ContainerFormat); string extension = ""; if (encoder is BmpBitmapEncoder) { extension = "bmp"; } else if (encoder is JpegBitmapEncoder) { extension = "jpg"; } else if (encoder is PngBitmapEncoder) { extension = "png"; } else if (encoder is TiffBitmapEncoder) { extension = "tif"; } else { encoder = null; } if (encoder != null) { bitmapName = bitmapName + '.' + extension; } } } if (encoder == null) { if (Microsoft.Internal.AlphaFlattener.Utility.NeedPremultiplyAlpha(value as BitmapSource)) { encoder = new WmpBitmapEncoder(); bitmapName = bitmapName + ".wmp"; } else { encoder = new PngBitmapEncoder(); bitmapName = bitmapName + ".png"; } } if (value is BitmapFrame) { encoder.Frames.Add((BitmapFrame)value); } else { encoder.Frames.Add(BitmapFrame.Create((BitmapSource)value)); } int index = m_mainFile.LastIndexOf('.'); if (index < 0) { index = m_mainFile.Length; } string uri = m_mainFile.Substring(0, index) + "_" + bitmapName; Stream bitmapStreamDest = new System.IO.FileStream(uri, FileMode.Create, FileAccess.ReadWrite, FileShare.None); #if DEBUG Console.WriteLine("Saving " + uri); #endif encoder.Save(bitmapStreamDest); bitmapStreamDest.Close(); return(new Uri(BaseUriHelper.SiteOfOriginBaseUri, uri)); }
public JpegBitmapEncoderFactory(int qualityLevel) { _qualityLevel = qualityLevel; _codecInfo = (new JpegBitmapEncoder()).CodecInfo; }