Sample code for 30+ languages & platforms
SQL Server Requires Chilkat v11.7.0+

Generate a QR Code PNG in Memory

See more QR Code Examples

Demonstrates getting a QR code image without writing a file. With no output options the result is a PNG returned as base64 in outputs[0].base64 of the response. The example decodes it into a BinData, which holds the bytes of the PNG to send in an HTTP response, store in a database, attach to an email, or save.

Chilkat SQL Server Downloads

SQL Server
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    DECLARE @iTmp1 int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    --  This example requires the Chilkat API to have been previously unlocked.
    --  See Global Unlock Sample for sample code.

    DECLARE @json int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'payload.text', 'https://www.chilkatsoft.com/'
    EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'render.moduleSize', 6

    DECLARE @fmt int
    EXEC @hr = sp_OACreate 'Chilkat.Formats', @fmt OUT

    DECLARE @jsonOut int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jsonOut OUT

    EXEC sp_OAMethod @fmt, 'QrCode', @success OUT, 'generate', @json, @jsonOut
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @fmt, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @fmt
        EXEC @hr = sp_OADestroy @jsonOut
        RETURN
      END


    EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'outputs[0].format'
    PRINT 'format: ' + @sTmp0

    EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'outputs[0].mimeType'
    PRINT 'MIME type: ' + @sTmp0

    EXEC sp_OAMethod @jsonOut, 'IntOf', @iTmp0 OUT, 'outputs[0].width'

    EXEC sp_OAMethod @jsonOut, 'IntOf', @iTmp1 OUT, 'outputs[0].height'

    PRINT 'image: ' + @iTmp0 + ' x ' + @iTmp1 + ' pixels'

    --  outputs[0].base64 is the PNG file, base64 encoded.
    DECLARE @bdPng int
    EXEC @hr = sp_OACreate 'Chilkat.BinData', @bdPng OUT

    EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'outputs[0].base64'
    EXEC sp_OAMethod @bdPng, 'AppendEncoded', @success OUT, @sTmp0, 'base64'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @bdPng, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @fmt
        EXEC @hr = sp_OADestroy @jsonOut
        EXEC @hr = sp_OADestroy @bdPng
        RETURN
      END

    EXEC sp_OAGetProperty @bdPng, 'NumBytes', @iTmp0 OUT
    PRINT 'PNG bytes: ' + @iTmp0

    --  The BinData holds the image.  Here it is simply saved.
    EXEC sp_OAMethod @bdPng, 'WriteFile', @success OUT, 'qa_output/qr_in_memory.png'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @bdPng, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @fmt
        EXEC @hr = sp_OADestroy @jsonOut
        EXEC @hr = sp_OADestroy @bdPng
        RETURN
      END

    --  The PNG held in bdPng:

    --  image

    --  Sample output:

    --  format: png
    --  MIME type: image/png
    --  image: 222 x 222 pixels
    --  PNG bytes: 877

    EXEC @hr = sp_OADestroy @json
    EXEC @hr = sp_OADestroy @fmt
    EXEC @hr = sp_OADestroy @jsonOut
    EXEC @hr = sp_OADestroy @bdPng


END
GO