2016-01-12 19 views
5

私のプログラムは画像をバッチ処理します。画像のexifオリエンテーションタグを読み込み、いくつかの処理及び任意のEXIFオリエンテーションタグなしで画像を保存しますが、適切な回転と(または正しい向きでEXIFタグ付き)EXIFオリエンテーションタグを取得し、適切な向きに回転し、画像を処理し、正しい向きで画像を保存します。

私はEXIFを読んで、this libraryを使用して回転しています:。

var bmp = new Bitmap(pathToImageFile); 
    var exif = new EXIFextractor(ref bmp, "n"); // get source from http://www.codeproject.com/KB/graphics/exifextractor.aspx?fid=207371 

    if (exif["Orientation"] != null) 
    { 
    RotateFlipType flip = OrientationToFlipType(exif["Orientation"].ToString()); 

    if (flip != RotateFlipType.RotateNoneFlipNone) // don't flip of orientation is correct 
    { 
    bmp.RotateFlip(flip); 
    bmp.Save(pathToImageFile, ImageFormat.Jpeg); 
    } 

    // Match the orientation code to the correct rotation: 

    private static RotateFlipType OrientationToFlipType(string orientation) 
    { 
    switch (int.Parse(orientation)) 
    { 
    case 1: 
    return RotateFlipType.RotateNoneFlipNone; 

    case 2: 
    return RotateFlipType.RotateNoneFlipX; 

    case 3: 
    return RotateFlipType.Rotate180FlipNone; 
    case 4: 
    return RotateFlipType.Rotate180FlipX; 
    break; 
    case 5: 
    return RotateFlipType.Rotate90FlipX; 
    break; 
    case 6: 
    return RotateFlipType.Rotate90FlipNone; 
    case 7: 
    return RotateFlipType.Rotate270FlipX; 
    case 8: 
    return RotateFlipType.Rotate270FlipNone; 
    default: 
    return 
} 
} 

これは動作します。この画像を保存すると、画像を間違った向きにするexif回転タグがまだ存在します。私は何ができるか は

 var bmp = new Bitmap(OS.FileName);  
     var exif = new EXIFextractor(ref bmp, "n"); 
     exif.setTag(0x112, "1"); 
     bmp.save("strippedexifimage"); 

である。しかしループで使用されるこのコードが50%前後.Is別の方法で私のプログラムを遅くしますか?ローテーションを適用した後にイメージを回転させるためのコードがあります。

更新:

@Hansは、あなたの答えの作品をアンパッサンが、それが生み出す結果は、ライブラリーによって生成されるものと同じです。 bitmap.save()を使用すると、ライブラリとコードの両方が動作します。しかし、次のコードを使用して、ユーザーが選択したフォーマットに応じてイメージを保存します。 Imgformatはimgformat = "image/png";,imgformat = "image/jpeg";などですが、一部の画像はまだ間違ったexif方向タグで保存されます。私はWindowsのエクスプローラで間違った画像のプレビューを参照してください、私はMSのペイントで画像を開くと、画像が正しいorientation.What私は間違っている?助けてください。機能を実装するためのライブラリを使用し

private void saveJpeg(string path, Bitmap img, long quality) 
     { 


      EncoderParameter qualityParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality); 


      ImageCodecInfo Codec = this.getEncoderInfo(imgformat); 
      if (Codec == null) 
       return; 

      EncoderParameters encoderParams = new EncoderParameters(1); 
      encoderParams.Param[0] = qualityParam;  
      img.Save(path + ext, Codec, encoderParams); 
     } 



private ImageCodecInfo getEncoderInfo(string mimeType) 
     { 
      // Get image codecs for all image formats 
      ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders(); 

      // Find the correct image codec 
      for (int i = 0; i < codecs.Length; i++) 
       if (codecs[i].MimeType == mimeType) 
        return codecs[i]; 
      return null; 
     } 
+0

ImageMagickをご覧ください。私はそれがあなたが望むすべてをすると思う。あなたはそれのためのコードを書くことができます..そのまま使用することができます。 :) – atlaste

答えて

5
var exif = new EXIFextractor(ref bmp, "n") 

あなたに多くの時間を節約することができます。また、図書館の設計が難しく、使用が困難な場合は、数日間は立ち往生することになります。 ref bmpが最初の大きな警告ですが、その値を文字列として指定しようとすると絶望のピットになりました。どのタイプが正しいsetTag()オーバーロードで何を意味するかを考慮する必要がなかったので、魅力的でした。それはタイプ3であり、2つの要素を持つバイト[]が必要です。これはまったく発見できません。ライブラリを必要としないときにのみ正しく使用することができます。

ライブラリを削除すると役立たない。方向を格納するEXIFタグは、0x112のIDを持ち、16ビットの値としてエンコードされます。 System.Drawingを直接使用して値を読み取り、1に戻します。このように:

static void FixImageOrientation(Image srce) { 
    const int ExifOrientationId = 0x112; 
    // Read orientation tag 
    if (!srce.PropertyIdList.Contains(ExifOrientationId)) return; 
    var prop = srce.GetPropertyItem(ExifOrientationId); 
    var orient = BitConverter.ToInt16(prop.Value, 0); 
    // Force value to 1 
    prop.Value = BitConverter.GetBytes((short)1); 
    srce.SetPropertyItem(prop); 

    // Rotate/flip image according to <orient> 
    switch (orient) { 
     // etc... 
    } 
} 
+0

ありがとうたくさんありました。私は、コードを介して直接行う方法があるかどうかを調べましたが、anyを見つけることができませんでした。私のスイッチのケースにある 'orient'戻り値(私は1,2,3を意味します。 .etc) – techno

+0

アップデートをご覧ください。 – techno

+0

あなたは私のアップデートの先生にあなたの意見をお寄せですか? – techno

関連する問題