How to resize image independent of size?

Asked

Viewed 757 times

2

Follow the code below:

public FileContentResult Foto_Pequeno()
{
    byte[] byte_image = null;
    string query = "SELECT * FROM Imagem WHERE Id = '1'";
    using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
    using (var command = new SqlCommand(query, connection))
    {
        connection.Open();
        using (var reader = command.ExecuteReader())
        {
            if (reader.Read())
            {
                byte_image = (byte[])reader["Image"];
            }
        }
    }
    return new FileContentResult(byte_image, "image/png");
}

In the database is written as varbinary(MAX), the image size is 946x456. How do I resize the image if it is larger than 100x100. As height and width.

It is possible to resize image byte array to 100 x 100 ?

  • Do you have these 946x456 values as default? another question 100 x 100 will not get distorted? a crop up to the size!!!

  • Yes 946x456 is the default size. 100x100 is just a test. I want an example of decreasing the image size.

  • In this case Where Id = 1 has default size of 946x456, if Where Id = 2 has another size.

1 answer

2


With class Webimage and the command Resize can successfully resize your image.

Minimal Example:

public FileContentResult Imagem()
{
    byte[] content = System.IO.File.ReadAllBytes(Server.MapPath("~/Image/") + "1.jpg");
    WebImage webImage = new WebImage(content);
    webImage.Resize(100, 100, true, false);
    content = webImage.GetBytes();
    return new FileContentResult(content, "image/jpg");
}

In your particular case:

public FileContentResult Foto_Pequeno()
{
    byte[] byte_image = null;
    string query = "SELECT * FROM Imagem WHERE Id = '1'";
    using (var connection = new SqlConnection(
                      ConfigurationManager
                               .ConnectionStrings["ConnectionString"].ConnectionString))
    using (var command = new SqlCommand(query, connection))
    {
        connection.Open();
        using (var reader = command.ExecuteReader())
        {
            if (reader.Read())
            {
                byte_image = (byte[])reader["Image"];
            }
        }
    }    
    WebImage webImage = new WebImage(byte_image);
    webImage.Resize(100, 100, true, false);
    byte_image = webImage.GetBytes();
    return new FileContentResult(byte_image, "image/jpg");
}

This class also has the methods:

  • Crop (clipping),
  • AddImageWatermark (watermark) and
  • AddTextWatermark (text as watermark).

References:

  • 1

    I didn’t know this "Webimage" worked right here.

  • 1

    One question, Webimage allows changing only by width ?

  • 1

    is yes by size, because?

  • 1

    Not at all, it’s just a question

Browser other questions tagged

You are not signed in. Login or sign up in order to post.