画像をトリミングしようとしています。これを行う方法は複数ありますが、希望どおりに実行できる方法はありません。画像がトリミングされたら、PDF ジェネレーターに送信します。通常の jpg を送信すると問題なく動作しますが、画像をトリミングすると、正しいサイズで PDF に送信されません。解像度に関係している可能性があると思います。
HTML ビューでは問題なく表示されますが、PDF にパブリッシュすると、イメージが予想よりも小さくなります。
私が使用しているトリミングコードは次のとおりです。
try
{
System.Drawing.Image image = System.Drawing.Image.FromFile(img);
Bitmap bmp = new Bitmap(width, height, PixelFormat.Format24bppRgb);
bmp.SetResolution(image.HorizontalResolution, image.VerticalResolution);
Graphics gfx = Graphics.FromImage(bmp);
gfx.SmoothingMode = SmoothingMode.AntiAlias;
gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
gfx.PixelOffsetMode = PixelOffsetMode.HighQuality;
gfx.DrawImage(image, new Rectangle(0, 0, width, height), x, y, width, height, GraphicsUnit.Pixel);
// Dispose to free up resources
image.Dispose();
//bmp.Dispose();
gfx.Dispose();
return bmp;
}
catch (Exception ex)
{
//MessageBox.Show(ex.Message);
return null;
}
私もこれを試しました:
Bitmap temp = (Bitmap)System.Drawing.Image.FromFile(img);
Bitmap bmap = (Bitmap)temp.Clone();
if (xPosition + width > temp.Width)
width = temp.Width - xPosition;
if (yPosition + height > temp.Height)
height = temp.Height - yPosition;
Rectangle rect = new Rectangle(xPosition, yPosition, width, height);
temp = (Bitmap)bmap.Clone(rect, bmap.PixelFormat);
これをコンテキストストリームに書き出しています:
Bitmap bm = Helper.CropImage(@"MyFileLocation", 0, 0, 300, 223);
context.Response.ContentType = "image/jpg";
bm.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
bm.Dispose();
興味深いことに、tiff イメージを試してコンテキスト タイプを変更すると、一般的な GDI+ エラーが発生します。調査によると、これはシークの問題のように見えますが、解決方法もわかりません。