1

I generate a QR code from code, then superimpose it over another image. Thie issue is that it doesn't line up as I expected.

/// <summary>
/// Draw one bitmap over another.
/// </summary>
/// <param name="largeBmp"></param>
/// <param name="smallBmp"></param>
/// <returns></returns>
public Bitmap Superimpose(Bitmap largeBmp, Bitmap smallBmp) {

    Graphics g = Graphics.FromImage(largeBmp);
    g.CompositingMode = CompositingMode.SourceOver;
    
    // Resize it so it fists
    smallBmp = ResizeBitmap(smallBmp, smallBmp.Width / 4, smallBmp.Height / 4);

    var x = (largeBmp.Width -  smallBmp.Width) / 2;
    var y = (largeBmp.Height - smallBmp.Height)/ 2;

    g.DrawImage(smallBmp, new Point(x, y));

    return largeBmp;
}

/// <summary>
/// Resize the bitmap
/// </summary>
/// <param name="bmp"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <returns></returns>
public Bitmap ResizeBitmap(Bitmap bmp, int width, int height)
{
    Bitmap result = new Bitmap(width, height);
    using (Graphics g = Graphics.FromImage(result))
    {
        g.DrawImage(bmp, 0, 0, width, height);
    }

    return result;
}

Non-Aligned Image

CorrieJanse
  • 2,374
  • 1
  • 6
  • 23

1 Answers1

0

I tried your code in win forms project and code worked fine, I used png format for large and small images, I think you should check your QR code, it might have some blank space around it

enter image description here

Surinder Singh
  • 1,165
  • 1
  • 3
  • 11