简体   繁体   English

我想从数据库中检索图像并根据用户需要裁剪它

[英]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 如果有,那么有任何教程或帮助可以指导我达到我的最终要求。请帮助我理解如何继续我的查询

Take a look at GDI+ libraries and also the System.Drawing namespace. 看看GDI +库以及System.Drawing命名空间。 You can also use Windows Imaging Components. 您还可以使用Windows Imaging Components。

Here's a basic walk-through . 这是一个基本的演练

使用Bitmap类可以轻松调整图像大小,看看这个构造函数 - http://msdn.microsoft.com/en-us/library/0wh0045z.aspx

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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