Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot use 5 argument version of QRCode constructor under Windows .net 8.0 with Nuget 1.5.1 #536

Closed
catdsnny opened this issue May 24, 2024 · 9 comments
Labels

Comments

@catdsnny
Copy link

catdsnny commented May 24, 2024

Type of issue

[x] Bug
[ ] Question (e.g. about handling/usage)
[ ] Request for new feature/improvement

Expected Behavior

Should be able to use 5 argument version of constructor

Current Behavior

The 5 argument version of the constructor does not exist in Nuget package v 1.5.1

Possible Solution (optional)

Update the Nuget package with current version of code

Steps to Reproduce (for bugs)

Create a new C# console solution with .NET 8.0
Add QRCode 1.5.1 with Nuget
Try to call the 5 argument version of QRCode constructor to make an QR with an embedded image.
Get compile error

Your Environment

Windows 10
.NET 8.0 Standard
Nuget v 1.5.1

@codebude
Copy link
Owner

When talking about QrCode constructor you mean the constructor of the QrCode renderer class? If so, this has no overload with 5 arguments. Please check the wiki: https://github.com/codebude/QRCoder/wiki/Advanced-usage---QR-Code-renderers#21-qrcode-renderer-in-detail

To give you a more detailed answer it would be helpful if you could post a short snippet of your code that throws the error.

@catdsnny
Copy link
Author

catdsnny commented May 24, 2024 via email

@codebude
Copy link
Owner

codebude commented May 27, 2024

Ok, I re-checked it with a simple test application:

QRCodeGenerator qrGenerator = new QRCodeGenerator();
QRCodeData qrCodeData = qrGenerator.CreateQrCode("The text which should be encoded.", QRCodeGenerator.ECCLevel.Q);
QRCode qrCode = new QRCode(qrCodeData);
var bmp = (Bitmap)Bitmap.FromFile(@"C:\\test.png");
Bitmap qrCodeImage = qrCode.GetGraphic(20, Color.Black, Color.White, bmp, 15);
pictureBox1.BackgroundImageLayout = ImageLayout.Zoom;
pictureBox1.BackgroundImage = qrCodeImage;

The application runs perfectly fine:

image

All four overloads are showing up:

image

Enviroment was QRCoder 1.5.1 from Nuget, .NET8 as target framework and Windows as development environment;

image

I can't see a bug here. The problem seems to be in your setup.

@andreflourenco
Copy link

I have the same problem in NET 6. I've created a very simple console application:

Program.cs

using System.Drawing;
using System.Drawing.Imaging;

byte[] qrCodeImageContent = null;
using (var qrGenerator = new QRCodeGenerator())
{
	using (var qrCodeData = qrGenerator.CreateQrCode("HELLO", QRCodeGenerator.ECCLevel.M))
	{
		using (var qrCode = new QRCode(qrCodeData))
		{
			var qrCodeBitmap = qrCode.GetGraphic(10, Color.Black, Color.White, false);
			using (var buffer = new MemoryStream())
			{
				qrCodeBitmap.Save(buffer, ImageFormat.Png);
				qrCodeImageContent = buffer.ToArray();
			};
		}
	}
}

QRCodeTest.csproj

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="QRCoder" Version="1.5.1" />
    <PackageReference Include="System.Drawing.Common" Version="8.0.6" />
  </ItemGroup>

</Project>

The error in console is:

error CS0246: The type or namespace name 'QRCode' could not be found (are you missing a using directive or an assembly reference?)

@Shane32
Copy link
Contributor

Shane32 commented Jun 11, 2024

Change this line as follows:

<TargetFramework>net6.0-windows</TargetFramework>

@andreflourenco
Copy link

Change this line as follows:

<TargetFramework>net6.0-windows</TargetFramework>

That would make my code platform specific, right?

It wasn't before and I would not like it to be...

@Shane32
Copy link
Contributor

Shane32 commented Jun 11, 2024

The QRCode class uses System.Drawing.Bitmap which is unavailable on non-Windows platforms starting with .NET 6. Please use a cross-platform renderer such as PngByteQRCode instead. See chart of available renderers here:

For reference, also see MS docs on System.Drawing.Common:

@andreflourenco
Copy link

andreflourenco commented Jun 11, 2024

OK, so using PngByteQRCode I reckon it would look something like:

using QRCoder;
using System.Drawing;

byte[] qrCodeImageContent = null;
using (var qrGenerator = new QRCodeGenerator())
{
	using (var qrCodeData = qrGenerator.CreateQrCode("HELLO", QRCodeGenerator.ECCLevel.M))
	{
		using (var qrCode = new PngByteQRCode(qrCodeData))
		{
			qrCodeImageContent = qrCode.GetGraphic(10, Color.Black, Color.White, false);
		}
	}
}

But could not figure out what would be the equivalent for the color parameters, since now it is a byte array. Can they be obtained from the System.Drawing.Color instances?

@Shane32
Copy link
Contributor

Shane32 commented Jun 11, 2024

var color = System.Drawing.Color.Red;
var darkColorRgba = new byte[] { color.R, color.G, color.B, color.A };

There could probably be an overload for this added...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants