How to create a banner by extending an Image in C #

1

I'm developing a method for adding a purple stripe to images that are rendered in my application. I'm using C # to draw in the image, currently I do this:

using (Image image = Image.FromFile(originalFilePath))
{
    Graphics imageGraphics = Graphics.FromImage(image);
    Rectangle FooterRectangle = new Rectangle(0, image.Height - 50, image.Width, 50); // image.height = 450px   image.width = 450px
    SolidBrush footerBrush = new SolidBrush(Color.FromArgb(189, 5, 59));
    imageGraphics.FillRectangle(footerBrush, FooterRectangle);
}

The result is this (Ignore watermark and text, focus on purple):

Until then beauty, the problem is, the track is overlapping the image, I need it to increase the height. For example, the image has 450px x 450px I need it to stay 450px x 500px ie I will not be cutting a part of the photo. Is there a way to instead of incrementing in the height of the photo?

    
asked by anonymous 11.08.2017 / 15:36

1 answer

1

You should create a new, larger-sized image and redraw your image above:

using (Image image = Image.FromFile(originalFilePath))
{
    System.Drawing.Bitmap novaImagem = new System.Drawing.Bitmap(image.Width, image.Height + 50); //sendo 50 o a altura da sua 'faixa'

    using (Graphics g = Graphics.FromImage(novaImagem)) //o Graphics vem da nova imagem
    {
        g.Clear(Color.FromArgb(189, 5, 59)); //"limpo" a nova imagem, e deixo ela toda na cor desejada (isso não é roxo, rsrs)

        g.DrawImage(image, new Rectangle(0,0,image.Width,image.Height));
        //Aqui, se quiser, você pode colocar o DrawString e escrever o texto...
    }

    return novaImagem;
}
    
11.08.2017 / 18:01