30 Aralık 2010 Perşembe

Convert Image File To MimeType

Bu Code Snippet'ın işimize yarayacağına eminim çünkü herhangi bir component yada dll kullanmadan convert edebilmemizi sağlıyor..
public static byte[] ConvertImage(byte[] fromImage, string mimeType)
  
{
    // Read the image from the byte variable into a bitmap variable
    MemoryStream fromImageStream = new MemoryStream();
    fromImageStream.Write(fromImage, 0, fromImage.Length);
    Image image = Image.FromStream( fromImageStream, true ) ;
    Bitmap bitmap = (Bitmap) image;
    // Instantiate the encoder
    EncoderParameters encoderParams = new EncoderParameters();
    encoderParams.Param[0] = new EncoderParameter( Encoder.Quality, 50L );
    ImageCodecInfo codecInfo = GetEncoderInfo( mimeType );
    MemoryStream newImage = new MemoryStream();
    // Convert the image to the new format
    bitmap.Save( newImage, codecInfo, encoderParams );
    // Read the new image into a byte variable
    byte[] data = newImage.ToArray();
    return data;
}

private static ImageCodecInfo GetEncoderInfo(String mimeType)
  {
   int j;
   ImageCodecInfo[] encoders;
   encoders = ImageCodecInfo.GetImageEncoders();
   for(j = 0; j < encoders.Length; ++j)
   {
    if(encoders[j].MimeType == mimeType)
     return encoders[j];
   }
   return null;
  }

Hiç yorum yok:

Yorum Gönder