0

我有一个将 byte[] 转换为图像的代码,但我的代码总是以横向模式写入,即使原始图像是纵向的。如何检测原始图像的页面方向并使用此属性编写新图像?有什么建议么?

public void SendFax(byte[] file, string fileName)
    {
        try
        {
            MemoryStream ms = new MemoryStream(file);
            Image imgSave = Image.FromStream(ms);
            Bitmap bmSave = new Bitmap(imgSave);
            Bitmap bmTemp = new Bitmap(bmSave);
            Graphics grSave = Graphics.FromImage(bmTemp);
            grSave.DrawImage(imgSave, 0, 0, imgSave.Width, imgSave.Height)

            //Save Image to physical path
            bmTemp.Save("C:/..." + fileName);

            imgSave.Dispose();
            bmSave.Dispose();
            bmTemp.Dispose();
            grSave.Dispose();

        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
4

1 回答 1

0

试试这个。检查 img 的高度和宽度,并根据比较决定纵向/横向

int srcWidth = image.Width;
int srcHeight = image.Height;
int thumbWidth = width;
int thumbHeight;
Bitmap bmp;
if (srcHeight > srcWidth)
{
    thumbHeight = (srcHeight / srcWidth) * thumbWidth;
    bmp = new Bitmap(thumbWidth, thumbHeight);
}
else
{
    thumbHeight = thumbWidth;
    thumbWidth = (srcWidth / srcHeight) * thumbHeight;
    bmp = new Bitmap(thumbWidth, thumbHeight);
}
于 2013-10-14T23:09:45.690 回答