2

Segue o código abaixo:

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

No banco de dados está gravado como varbinary(MAX), o tamanho da imagem está como 946x456. Como faço para redimensionar a imagem se ela for maior que 100x100. Como altura e largura.

É possivel redimensionar image byte array para 100 x 100 ?

3
  • Você tem esses valores 946x456 como padrão? outra pergunta 100 x 100 não vai ficar distorcido? um recorte até até dá pelo tamanho!!!
    – novic
    3/01/2017 às 19:22
  • Sim 946x456 é o tamanho padrão. O 100x100 é só um teste. Quero um exemplo de diminuir o tamanho da imagem. 3/01/2017 às 19:27
  • Nesse caso de where Id = 1 tem tamanho padrão de 946x456, se for where Id = 2 tem outro tamanho diferente. 3/01/2017 às 19:29

1 Resposta 1

2

Com a classe WebImage e o comando Resize pode conseguir com êxito redimensionar sua imagem.

Exemplo Minimo:

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

No seu caso em particular:

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

Essa classe também possui o métodos:

  • Crop (recorte),
  • AddImageWatermark (marca d'agua) e
  • AddTextWatermark (texto como marca d'agua).

Referencias:

4
  • 1
    Não sabia esse "WebImage" trabalhou certinho aqui. 3/01/2017 às 19:37
  • 1
    Uma dúvida, WebImage permite alterar só pelo width ? 3/01/2017 às 19:57
  • 1
    é sim pelo tamanho, porque?
    – novic
    3/01/2017 às 20:03
  • 1
    Por nada, é só uma pergunta 3/01/2017 às 20:04

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .