简体   繁体   中英

How to rotate an image which is retrieved by ashx handler

I have an image which is returned from MySql. Its datatype is image in Mysql. I handle the image with ashx. the code below show my ashx file.

string imageID = context.Request.QueryString["imageID"];
using(SqlConnection objConnection = new SqlConnection(portalConnectionString))
{
    objConnection.Open();
    SqlCommand fetchTaskImageCMD = new SqlCommand("fetch_task_image", objConnection);
    fetchTaskImageCMD.CommandType = CommandType.StoredProcedure;
    fetchTaskImageCMD.Parameters.Add("@IMAGE_ID", SqlDbType.VarChar).Value = imageID;

    try
    {
        SqlDataReader dr = fetchTaskImageCMD.ExecuteReader();
        dr.Read();

        byte[] bufferImg = (Byte[])dr[0];

        context.Response.OutputStream.Write(bufferImg, 0, bufferImg.Length);
    }

    catch
    {
        System.Diagnostics.Debug.WriteLine("Image Handler Failed : /DesktopModules/ImageHandler.ashx");
    }

    objConnection.Close();
}

And the code below is showing how I am calling my ashx file from my asp.net code behind class in c#.

protected void Page_Load(object sender, System.EventArgs e)
{
   FetchedImage.ImageUrl = "/DesktopModules/ImageHandler.ashx?imageType=" + imageType + "&imageID=" + imageID;
}

My question is how I am going to rotate this image and save back to database,then.

Any comment will be appreciated.

If you want to rotate it and save it back to the database, use GDI+ on the server instead of streaming it back to the client.

System.Drawing.Image can be instantiated from a byte array, and from that you can use GDI+ methods to rotate the image, extract a byte array from the rotated image and save that.

Let me know if you need an example, but googling System.Drawing.Image will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM