I want to get an image from a database and crop it according to user needs

I want to crop the photo obtained from the database, I did the following to get the image from the database

protected void Page_Load(object sender, EventArgs e)
{
    MemoryStream stream = new MemoryStream();
    SqlConnection connection = new SqlConnection(@"Data Source=localhost;Initial Catalog=card;User Id=sa;Password=db2admin;");
    try
    {
        connection.Open();
        SqlCommand command = new SqlCommand("select Photo from iffcar", connection);
        byte[] image = (byte[])command.ExecuteScalar();
        stream.Write(image, 0, image.Length);
        Bitmap bitmap = new Bitmap(stream);
        Response.ContentType = "image/gif";
        bitmap.Save(Response.OutputStream, ImageFormat.Jpeg);
    }
    catch (Exception ee)
    {
        connection.Close();
        stream.Close(); 
        HttpContext.Current.Response.Write(ee.Message);
    }
}

The resulting image is displayed inside the browser.

Now I am fixated on how to crop this image, I want to allow the user to crop the image extracted from the database and save the cropped image, which will be transferred to the Crystal report.

Is it possible? If so, as there is any tutorial or help to help me in my end.please requirement, help me figure out how to proceed with my request.

+3
source share
3 answers

Complete and complete the following:

    connection.Open();
    SqlCommand command = new SqlCommand("select Photo from iffcar", connection);
    byte[] image = (byte[])command.ExecuteScalar();
    stream.Write(image, 0, image.Length);
    Bitmap bitmap = new Bitmap(stream);

    int croppedWidth = ??, croppedHeight = ??, cropOffsetX = ??, cropOffsetY = ??;
    var croppedBitmap = new Bitmap(croppedWidth, croppedHeight);
    var graphics = Graphics.FromImage(croppedBitmap);
    graphics.DrawImage(bitmap, -cropOffsetX, -cropOffsetY, bitmap.Width, bitmap .Height);
    Response.ContentType = "image/gif";
    croppedBitmap.Save(Response.OutputStream, ImageFormat.Jpeg);
0

GDI +, System.Drawing. Windows.

.

+1

Yo can easily resize the image using the Bitmap class, look at this constructor - http://msdn.microsoft.com/en-us/library/0wh0045z.aspx

0
source

All Articles