Ishan
Ishan

Reputation: 4028

I want to retrieve an Image from database and crop it as per the user needs

i want to crop a photo retrieved from database, i have done the following to retrieve 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 image that is retrieved is displayed inside the browser.

Now i am stuck at how to crop this image,i want to allow the user to crop the image retrieved from the database and store the cropped image to be passed onto crystal report.

Is this possible? If so than is there any tutorial or help which would guide me to my end requirement.please help me to understand how to proceed with my query

Upvotes: 3

Views: 1180

Answers (3)

immutabl
immutabl

Reputation: 6903

Take a look at GDI+ libraries and also the System.Drawing namespace. You can also use Windows Imaging Components.

Here's a basic walk-through.

Upvotes: 1

Tim Rogers
Tim Rogers

Reputation: 21713

Fill in the ??s and try this:

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

Upvotes: 0

Anton Semenov
Anton Semenov

Reputation: 6347

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

Upvotes: 0

Related Questions