0

現在、GDI ( System.Drawing) を使用した作業コードがあります。しかし、これを変換してSystem.Windows.Media.Imaging、またはImageMagickを使用することを検討しています

私の懸念は、これがメモリをリークしないこと、スレッドセーフでマルチスレッドであること、そして高品質の結果を提供することです。ImageMagick はこれらすべてを提供しているようです。ただし、System.Windows.Media.Imaging「よりクリーンな」ソリューションとして提案されています。

これらの方法のいずれかに落とし穴があることを知っていますか?

私が見なければならない他のオプションはありますか?

4

1 に答える 1

0

私はこのルーチンを私のために働いています

public Bitmap FitImage(Image imgPhoto, int Width, int Height)
{
  int sourceWidth = imgPhoto.Width;
  int sourceHeight = imgPhoto.Height;
  int sourceX = 0;
  int sourceY = 0;
  int destX = 0;
  int destY = 0;

  float nPercent = 0;
  float nPercentW = 0;
  float nPercentH = 0;

  nPercentW = ((float)Width / (float)sourceWidth);
  nPercentH = ((float)Height / (float)sourceHeight);

  if (nPercentH < nPercentW) {
    nPercent = nPercentW;
    destY = (int)((Height - (sourceHeight * nPercent)) / 2);
  } else {
    nPercent = nPercentH;
    destX = (int)((Width - (sourceWidth * nPercent)) / 2);
  }

  int destWidth = (int)Math.Round(sourceWidth * nPercent);
  int destHeight = (int)Math.Round(sourceHeight * nPercent);

  Bitmap newPhoto = new Bitmap(Width, Height, PixelFormat.Format24bppRgb);
  Graphics newgrPhoto = Graphics.FromImage(newPhoto);
  newgrPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic; 
  newPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

  newgrPhoto.PixelOffsetMode = PixelOffsetMode.Half;

  var attr = new ImageAttributes();
  attr.SetWrapMode(WrapMode.TileFlipXY);

  newgrPhoto.DrawImage(imgPhoto,
      new Rectangle(destX, destY, destWidth, destHeight),
      sourceX, sourceY, sourceWidth, sourceHeight,
      GraphicsUnit.Pixel,
      attr
   );

  newgrPhoto.Dispose();

  return newPhoto;
}

あなたが望むことを正確に行うことはできないかもしれませんが、一般的なアイデアは得られます。マルチスレッド環境で使用され、リークしません。

于 2013-03-14T13:40:36.933 に答える