0

What I need is to create a new bitmap with a bigger height and same width of a bitmap file, paste the bitmap file contents inside the new one and write a string on the bottom of the bitmap file.

Image example of how it needs to be.

This is my current code, I dont know how to write on the bottom of the file / neither pasting the contents of the bitmap into another, thanks for the help!:

Bitmap bmp = new Bitmap(filePathSave);

RectangleF rectf = new RectangleF(0, 0, bmp.Width, bmp.Height);
Graphics g = Graphics.FromImage(bmp);

g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;

StringFormat format = new StringFormat()
{
     Alignment = StringAlignment.Center,
     LineAlignment = StringAlignment.Center
};

g.DrawString("yourText", new Font("Tahoma", 20), Brushes.Black, rectf, format);

// Now save or use the bitmap
bmp.Save($"Temp/{Path.GetFileNameWithoutExtension(filePathSave)}_1.bmp", ImageFormat.Bmp);

// Flush all graphics changes to the bitmap
g.Flush();
  • You should do just what you planned. The new bitmap should not be a copy but a new one and larger. Then DrawImage the old one into it and the do the drawstring. so `new Bitmap(filePathSave);` should be `new Bitmap(newWidth, newHeight);` nad there should be a `g.DrawImage(oldBmp, 0, 0);` – TaW Oct 13 '20 at 09:29

1 Answers1

0

Basically what I did is use a void I found on stackoverflow to resize the image proportionally and later just do what @TaW told me. Thanks!

        using (var image = Image.FromFile(filePathSave))
        using (var newImage = Logic.ScaleImage(image, image.Width, image.Height + 125))
        {
            Bitmap destBitmap = new Bitmap(image.Width + 150, image.Height + 125, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
            Graphics g = Graphics.FromImage(destBitmap);

            g.SmoothingMode = SmoothingMode.AntiAlias;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.PixelOffsetMode = PixelOffsetMode.HighQuality;
            g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;

            g.Clear(Color.White);
            g.DrawImage(newImage, 0, 0);

            g.DrawString(sneakerName, new Font("Arial", 16), Brushes.Black, 5, image.Height + y);
            // Now save or use the bitmap
            //g.Save();
            destBitmap.Save($"{saveTo}/{Path.GetFileNameWithoutExtension(filePathSave)}.jpeg", ImageFormat.Jpeg);

            // Flush all graphics changes to the bitmap
            g.Flush();
        }