December 11, 2013 at 1:54 pm
I am using the QRCode4CS class (http://qrcode4cs.codeplex.com/releases/view/74015) to generate QR codes.
I can use the following code to successfully return a bitmap image to a picturebox in a Windows Form Application.
public class CreateQRCodeClass
{
public static Image CreateQRCodeImage(string inputString)
{
QRCode4CS.QRCode qrcode = new QRCode4CS.QRCode(new QRCode4CS.Options(inputString));
qrcode.Make();
Image canvas = new Bitmap(86, 86);
Graphics artist = Graphics.FromImage(canvas);
artist.Clear(Color.White);
for (int row = 0; row < qrcode.GetModuleCount(); row++)
{
for (int col = 0; col < qrcode.GetModuleCount(); col++)
{
bool isDark = qrcode.IsDark(row, col);
if (isDark == true)
{
artist.FillRectangle(Brushes.Black, 2 * row + 10, 2 * col + 10, 2 * row + 15, 2 * col + 15);
}
else
{
artist.FillRectangle(Brushes.White, 2 * row + 10, 2 * col + 10, 2 * row + 15, 2 * col + 15);
}
}
}
artist.FillRectangle(Brushes.White, 0, 76, 86, 86);
artist.FillRectangle(Brushes.White, 76, 0, 86, 86);
artist.Dispose();
return canvas;
}
}
In trying to adapt the same code (below) to display a QR code in an SSRS report I get the error "There is an error on line 1 of custom code: [BC30311] Value of type 'System.Drawing.Image' cannot be converted to '1-dimensional array of Byte.'
Here is the custom code I am using.
Public Function QRCode(ByVal RetailerId As String) as Byte()
Return QRCode4CSCreateQRCode.CreateQRCodeClass.CreateQRCodeImage(RetailerId)
End Function
Here is the revised custom assembly.
public class CreateQRCodeClass
{
public static byte[] CreateQRCodeImage(string inputString)
{
QRCode4CS.QRCode qrcode = new QRCode4CS.QRCode(new QRCode4CS.Options(inputString));
qrcode.Make();
Image canvas = new Bitmap(86, 86);
Graphics artist = Graphics.FromImage(canvas);
artist.Clear(Color.White);
for (int row = 0; row < qrcode.GetModuleCount(); row++)
{
for (int col = 0; col < qrcode.GetModuleCount(); col++)
{
bool isDark = qrcode.IsDark(row, col);
if (isDark == true)
{
artist.FillRectangle(Brushes.Black, 2 * row + 10, 2 * col + 10, 2 * row + 15, 2 * col + 15);
}
else
{
artist.FillRectangle(Brushes.White, 2 * row + 10, 2 * col + 10, 2 * row + 15, 2 * col + 15);
}
}
}
artist.FillRectangle(Brushes.White, 0, 76, 86, 86);
artist.FillRectangle(Brushes.White, 76, 0, 86, 86);
artist.Dispose();
System.IO.MemoryStream ms = new System.IO.MemoryStream();
canvas.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] imagedata = null;
imagedata = ms.GetBuffer();
return imagedata;
}
}
What data type can I successfully return to SSRS to display the image?
December 11, 2013 at 3:28 pm
How about you just return data with the SQL Server datatype [var]binary(n)?
This you can easily be displayed in Reporting Services reports.
Would this work for you?
Andreas
---------------------------------------------------
MVP SQL Server
Microsoft Certified Master SQL Server 2008
Microsoft Certified Solutions Master Data Platform, SQL Server 2012
www.insidesql.org/blogs/andreaswolter
www.andreas-wolter.com
December 11, 2013 at 8:24 pm
Andreas.Wolter (12/11/2013)
How about you just return data with the SQL Server datatype [var]binary(n)?This you can easily be displayed in Reporting Services reports.
Would this work for you?
Thanks for the response.
I am not sure. I have a bitmap in memory in PNG format in my assembly. Can that be converted to a [var]binary(n)?
December 12, 2013 at 2:53 am
I'll have to pass on that one
I am not a .Net developer.
All that SQL Server needs is the binary representation.
But what exact code you need in order to do that, I have no idea
Andreas
---------------------------------------------------
MVP SQL Server
Microsoft Certified Master SQL Server 2008
Microsoft Certified Solutions Master Data Platform, SQL Server 2012
www.insidesql.org/blogs/andreaswolter
www.andreas-wolter.com
December 12, 2013 at 5:43 am
Stan for the portion where you stick the image into a table, you can do it via a CLR function;
the CLR would look like this:
[Microsoft.SqlServer.Server.SqlFunction()]
public static SqlBytes CLR_GetFileImage(SqlString RetailerId)
{
try {
return new SqlBytes(QRCode4CSCreateQRCode.CreateQRCodeClass.CreateQRCodeImage(RetailerId));
} catch (SqlException sqlex) {
throw new Exception("", sqlex);
} catch (Exception ex) {
throw new Exception("", ex);
}
}
if QRCode4CSCreateQRCode is not a class that can be included in the project, but a compiled dll, you have to add it to your SQL server database, before you can add a reference to it for the CLR to use.
i'm not sure on the real/full assembly name, but here's an example; you might have to enable CLR, and set your database to trustworthy before you can ever get it to deploy.
--if the QRCode4CSCreateQRCode.CreateQRCodeClass assembly exists then drop it
IF NOT EXISTS EXISTS (SELECT * FROM sys.assemblies WHERE [name] = 'QRCode4CSCreateQRCode.CreateQRCodeClass')
BEGIN
CREATE ASSEMBLY [QRCode4CSCreateQRCode.CreateQRCodeClass] AUTHORIZATION dbo
FROM 'C:\Data\QRCode4CSCreateQRCode.dll'
WITH PERMISSION_SET = UNSAFE
END --IF EXISTS
Lowell
December 12, 2013 at 6:25 am
Andreas.Wolter (12/12/2013)
I'll have to pass on that oneI am not a .Net developer.
All that SQL Server needs is the binary representation.
But what exact code you need in order to do that, I have no idea
You pointed me in the right direction anyway. Thanks!
December 12, 2013 at 6:27 am
That's just what I was looking for. Thanks!
December 12, 2013 at 7:09 am
Lowell (12/12/2013)
... you might have to enable CLR, and set your database to trustworthy before you can ever get it to deploy.
...
[/code]
Thanks for jumping in with a code-sample
I would like to add though: Please do not set databases trustworthy without knowing the exact and complete security architecture.
In 90% of all cases this leads to a setup which allows for privilege elevation to sysadmin.
Use certificates instead if Unsafe Assemblies are necessary.
Andreas
---------------------------------------------------
MVP SQL Server
Microsoft Certified Master SQL Server 2008
Microsoft Certified Solutions Master Data Platform, SQL Server 2012
www.insidesql.org/blogs/andreaswolter
www.andreas-wolter.com
December 14, 2013 at 2:32 pm
I figured out the answer.
You have to convert the bitmap image to a byte array to display it in SSRS.
This is what the revised code looks like that can be used with SSRS.
public static Byte[] ReturnByteArray(string inputString)
{
QRCode4CS.QRCode qrcode = new QRCode4CS.QRCode(new QRCode4CS.Options(inputString));
qrcode.Make();
Image canvas = new Bitmap(86, 86);
Graphics artist = Graphics.FromImage(canvas);
artist.Clear(Color.White);
for (int row = 0; row < qrcode.GetModuleCount(); row++)
{
for (int col = 0; col < qrcode.GetModuleCount(); col++)
{
bool isDark = qrcode.IsDark(row, col);
if (isDark == true)
{
artist.FillRectangle(Brushes.Black, 2 * row + 10, 2 * col + 10, 2 * row + 15, 2 * col + 15);
}
else
{
artist.FillRectangle(Brushes.White, 2 * row + 10, 2 * col + 10, 2 * row + 15, 2 * col + 15);
}
}
}
artist.FillRectangle(Brushes.White, 0, 76, 86, 86);
artist.FillRectangle(Brushes.White, 76, 0, 86, 86);
artist.Dispose();
System.IO.MemoryStream ms = new System.IO.MemoryStream();
canvas.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
byte[] imagedata = null;
imagedata = ms.GetBuffer();
return imagedata;
}
June 26, 2014 at 8:04 pm
Sounds like an interesting question, I am recently new to the barcode software[/url], feel headaches on how to use the qr code in ssrs reports. I think I gained something from this topic.
June 26, 2014 at 8:58 pm
alex_2356 (6/26/2014)
Sounds like an interesting question, I am recently new to the barcode software[/url], feel headaches on how to use the qr code in ssrs reports. I think I gained something from this topic.
Here is an article I wrote a while back:
Generate two-dimensional QR Code® bar codes in an SSRS report[/url]
February 9, 2015 at 1:14 am
Stan Kulp-439977 (12/14/2013)
I figured out the answer.You have to convert the bitmap image to a byte array to display it in SSRS.
This is what the revised code looks like that can be used with SSRS[/url].
public static Byte[] ReturnByteArray(string inputString)
{
QRCode4CS.QRCode qrcode = new QRCode4CS.QRCode(new QRCode4CS.Options(inputString));
qrcode.Make();
Image canvas = new Bitmap(86, 86);
Graphics artist = Graphics.FromImage(canvas);
artist.Clear(Color.White);
for (int row = 0; row < qrcode.GetModuleCount(); row++)
{
for (int col = 0; col < qrcode.GetModuleCount(); col++)
{
bool isDark = qrcode.IsDark(row, col);
if (isDark == true)
{
artist.FillRectangle(Brushes.Black, 2 * row + 10, 2 * col + 10, 2 * row + 15, 2 * col + 15);
}
else
{
artist.FillRectangle(Brushes.White, 2 * row + 10, 2 * col + 10, 2 * row + 15, 2 * col + 15);
}
}
}
artist.FillRectangle(Brushes.White, 0, 76, 86, 86);
artist.FillRectangle(Brushes.White, 76, 0, 86, 86);
artist.Dispose();
System.IO.MemoryStream ms = new System.IO.MemoryStream();
canvas.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
byte[] imagedata = null;
imagedata = ms.GetBuffer();
return imagedata;
}
Thank you all posts!:-P
I also meet a error in creating qr code in ssrs reports.
So lucky found a resolution here.
Viewing 12 posts - 1 through 11 (of 11 total)
You must be logged in to reply to this topic. Login to reply