我怎样才能从数据库中旋转图像

时间:2014-11-05 02:09:34

标签: c# asp.net sql-server

我想旋转数据库中存储的图像是否可能? 我可以从数据库中获取图像,但我不知道如何旋转它。

    string img = (Request.QueryString["cn"]);
    Byte[] bytes = null;

    if (rbPhoto1.Checked == true)
    {
        if (img != null)
        {
            //string str = "select mem_contenttype, mem_photo from tblCardRequestDetail2 where mem_cardno = '" + Request.QueryString["cn"] + "'";
            string str = "select mem_contenttype1, mem_photo1 from tblphotoupload where mem_cardno = '" + img + "'";
            SqlCommand cmd = new SqlCommand(str); cmd.Parameters.Add("@1", SqlDbType.VarChar).Value = img;
            DataTable dt = GetData(cmd);


            bytes = (Byte[])dt.Rows[0]["mem_photo1"];
            Response.OutputStream.Write(bytes, 0, bytes.Length);
            Response.Buffer = true;
            Response.Charset = "";
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.ContentType = dt.Rows[0]["mem_contenttype1"].ToString();
            Response.AddHeader("content-disposition", "attachment;filename=" + dt.Rows[0]["mem_photo1"].ToString());
            Response.BinaryWrite(bytes);
            Response.Flush();
            //Response.End();
        }
    }

2 个答案:

答案 0 :(得分:1)

您可以尝试以下代码

var memStream = new MemoryStream(bytes);

Image imgFromStream = Image.FromStream(memStream, true);
imgFromStream.RotateFlip(RotateFlipType.Rotate90FlipNone);
imgFromStream.Save(memStream,System.Drawing.Imaging.ImageFormat.Jpeg);//Change to whichever format you need
bytes  =  imgFromStream.ToArray();

答案 1 :(得分:0)

您可以使用这样的辅助方法:

public static byte[] ReadRotateAndWriteBitmap(byte[] imageBytes)
{
    ImageConverter converter = new ImageConverter();
    using (Image img = (Image)converter.ConvertFrom(imageBytes))
    {
        if (img == null)
            return null;
        img.RotateFlip(RotateFlipType.Rotate90FlipNone);
        return (byte[])converter.ConvertTo(img, typeof(byte[]));
    }
}