4

I am using ZXing.Net 0.16.4.0 to decode qr code files which are kept inside the 'wwwroot/qrr' folder, but i am getting compile time error:

Cannot convert from 'System.Drawing.Bitmap' to 'ZXing.LuminanceSource'

My Code:

string[] files = Directory.GetFiles("wwwroot/qrr");
foreach (string file in files)
{
    // create a barcode reader instance
    IBarcodeReader reader = new BarcodeReader();
    // load a bitmap
    var barcodeBitmap = (Bitmap)Image.FromFile("wwwroot/qrr/" + Path.GetFileName(file));

    // detect and decode the barcode inside the bitmap
    var result = reader.Decode(barcodeBitmap);
}

The error is in the last line of code:

 var result = reader.Decode(barcodeBitmap);

My application is in ASP.NET Core and I am using the official docs code but they are not working. Please help?

Update

I noticed that the ZXing pacakage when added to ASP.NET Core application, then it's ZXing.IBarcodeReader has a missing Decode overloaded method:

IBarcodeReader has only 2 overloades of decode method:

Result Decode(byte[] rawRGB, int width, int height, RGBLuminanceSource.BitmapFormat format);
Result Decode(LuminanceSource luminanceSource);

But when you install this package in .Net 4.6.1 framework then there is one more overload method:

Result Decode(Bitmap barcodeBitmap);

How can an package gives different codes in different frameworks?

yogihosting
  • 5,494
  • 8
  • 47
  • 80

1 Answers1

5

Since you're using ASP.NET Core, I'm supposing you've added an reference to the package of ZXing.Net.Bindings.CoreCompat.System.Drawing . You need change your code as below :

string[] files = Directory.GetFiles("wwwroot/qrr");
foreach (string file in files)
{
    // create a barcode reader instance
    IBarcodeReader reader = new BarcodeReader();
    BarcodeReader reader = new BarcodeReader();
    // load a bitmap
    var barcodeBitmap = (Bitmap)Image.FromFile("wwwroot/qrr/" + Path.GetFileName(file));

    // detect and decode the barcode inside the bitmap
    var result = reader.Decode(barcodeBitmap);
}

Note this .Decode() is an extension method of IBarcodeReaderGeneric instead of the IBarcodeReader

itminus
  • 23,772
  • 2
  • 53
  • 88
  • unfortunately the error still remains in my application even thought I applied your modified code. Can you upload your application to github and send me it's url. Thank you! – yogihosting Jan 25 '19 at 10:40
  • 1
    @yogihosting See [a demo here](https://github.com/newbienewbie/SO-54329643/blob/master/Controllers/HomeController.cs#L24-L35) – itminus Jan 26 '19 at 01:13
  • I added ZXing.Net.Bindings.CoreCompat.System.Drawing from nuget https://www.nuget.org/packages/ZXing.Net.Bindings.CoreCompat.System.Drawing/. But after that I start getting weird error - The type 'Bitmap' exists in both 'CoreCompat.System.Drawing, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' and 'System.Drawing.Common, Version=4.0.0.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. Strangely, I check the the ZXing.CoreCompat.System.Drawing but there is no Bitmap class. – yogihosting Jan 28 '19 at 14:28
  • 1
    @yogihosting 1. As the name suggests, the `ZXing.Net.Bindings.CoreCompat.System.Drawing` uses the `CoreCompat.System.Drawing` package instead of the `System.Drawing.Common`. The `CoreCompat.System.Drawing` is a library that is released much earlier before `System.Drawing.Common` package becomes available on .NET Core . You will find the `Bitmap` class within the `CoreCompat.System.Drawing` library . – itminus Jan 29 '19 at 01:26
  • 1
    @yogihosting It is too long to post explanation within a single comment. 2. It seems that you have added another package reference on [System.Drawing.Common](https://www.nuget.org/packages/System.Drawing.Common/4.6.0-preview.18571.3) manually. If that's the case, you don't have to do that. Simply remove that dependency. See [my Demo here](https://github.com/newbienewbie/SO-54329643/blob/912092fbbad02739099570b1ff23d298fccd4f98/App.csproj#L10-L11) – itminus Jan 29 '19 at 01:27
  • I am not using System.Drawing.Common package. Using only ZXing.Net.Bindings.CoreCompat.System.Drawing. But my asp.net core 2.0 application gets the complier error Cannot convert from 'System.Drawing.Bitmap' to 'ZXing.LuminanceSource'. If you will create 2.0 core application then you will get the same error. Thank you. Also there is an extra Decode() overload in the assembly when installed in .NET 4.6.1 framework. I have described it on my question (please see it). – yogihosting Jan 29 '19 at 11:44
  • 1
    @yogihosting If you're using .NET Framework, please remove the `ASP.NET Core` tag – itminus Jan 29 '19 at 11:58
  • 1
    @yogihosting Also, the .NET Framework already has a `System.Drawing.Bitmap` itself. So there's no need to define such a compat class for .NET Framework – itminus Jan 29 '19 at 11:59
  • in my asp.net core 2.0 I added ZXing.Net.Bindings.CoreCompat.System.Drawing. Now the compile errors are gone. But i get run time error whenever I try to create qr code. The error is - Could not load type 'ZXing.BarcodeWriterPixelData' from assembly 'ZXing, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. – yogihosting Jan 29 '19 at 12:10
  • 1
    @yogihosting For using ZXing.NET with ASP.NET Core, the author has an awesome [sample here](https://github.com/micjahn/ZXing.Net/tree/master/Clients/ASP.NetCoreDemo). But that demo is not updated to ASP.NET Core 2.x. Someone asks a [similar how-to question here](https://stackoverflow.com/questions/54067764/generate-barcode-with-zxing-net/54070276#54070276), hope it helps. Note the way we create image. – itminus Jan 29 '19 at 12:29
  • Thank you itminus, My problem is solved with your help. I can now run ZXing.net perfectly in asp.net core 2.0 – yogihosting Jan 29 '19 at 12:36