如何使用横向或纵向属性将byte []写入图像?

时间:2013-07-31 11:32:45

标签: c#-4.0 image-processing bitmap memorystream

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

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;
        }
    }

1 个答案:

答案 0 :(得分: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);
}