How to resize image independent of size?

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 as 946x456. How do I resize the image if it is larger than 100x100. Like height and width.

Is it possible to resize image byte array to 100 x 100?

    
asked by anonymous 03.01.2017 / 20:10

1 answer

2

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

Minimum 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 methods:

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

03.01.2017 / 20:33