This project has moved. For the latest updates, please go here.

Alternative to LockBits due to Security issues

Mar 5, 2015 at 2:47 PM
Edited Mar 5, 2015 at 3:38 PM
Hi,

I'm trying to create a Barcode in CRM 2011 (On Premises) Reports. Unfortunately this isn't possible due to Sandbox and various other issues. I've tried a few methods but nothing works.

So my approach now, is to create a c# plugin that generates the barcode image when a record is created, and store the image byte as a string, and then use Convert.FromBase64String in the report to display the barcode image. This method should then hopefully work...

However, after days of trying, I'm stuck with an issue with Permission Security
System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
, and have debugged to a section in the Render that doesn't work / compatible (for CRM 2011..!)
var bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);
So my question is: Is there a way to do this without Lockbits?

Any help here would be greatly received.
Coordinator
Mar 5, 2015 at 7:37 PM
Perhaps it would be a better solution if you can avoid using the BitmapRenderer.
You can implement a custom renderer which generates an output format for your needs.
For example take a look at the RawRenderer which is available in the portable version of zxing.
The result of the RawRenderer is a byte array which contains the raw ARGB values without
any header or other format specifications.
Perhaps you can better use such a raw ARGB array within CRM 2011 instead of a stream
from the Bitmap class.
Mar 6, 2015 at 8:36 AM
Thanks, I'll give this a go today and update with my progress
Mar 6, 2015 at 3:38 PM
mmmm, so after spending all day on this, all I get is a black box image! So, I now have to give up for a while and come back to this another time.

Is there any examples of the RawRenderer being used?
When I was testing the use of ZXing.net, I used the following code in a SSRS project, where the report displays the barcode correctly. The issue was then importing the report into CRM 2011, with the issues as mentioned above.
                var writer = new BarcodeWriter
                {
                    Format = BarcodeFormat.CODE_128,
                    Options = new ZXing.Common.EncodingOptions
                    {
                        Width = 450,
                        Height = 100,
                        PureBarcode = true
                    }
                };

                var bmp = writer.Write(text);
So I've now tried to use RawRenderer but this doesn't seem to work (or I'm not using it right!) Any guidance on using would be great.

Notes: I'm on Win7, and the zxing.net.mobile sln only opens in Win8.1 (suggestions on how to open the project would also be great)
Coordinator
Mar 6, 2015 at 7:20 PM
If you don't use the assembly zxing.portable then you have to create a special BarcodeWriter class:
   public class BarcodeWriterRaw : BarcodeWriterGeneric<byte[]>
   {
      public BarcodeWriterRaw()
      {
         Renderer = new RawRenderer();
      }
   }
Now you can use the writer to create a raw ARGB representation of the barcode:
                var writer = new BarcodeWriterRaw
                {
                    Format = BarcodeFormat.CODE_128,
                    Options = new ZXing.Common.EncodingOptions
                    {
                        Width = 450,
                        Height = 100,
                        PureBarcode = true
                    }
                };

                var rawARGBImageData = writer.Write(text);
Remember, the result isn't a bitmap object. The result is an array with the pure color values for the channels
alpha, red, green and blue. But with the RawRenderer you have no issues with security and sandboxes.
But you have to look into SSRS how you can proceed with the raw data.

I'm not sure but it should be possible to create your own bitmap instance from the byte array:
fixed (byte* p = rawARGBImageData)
{
    IntPtr ptr = (IntPtr)p;
    Bitmap newBitmap = new Bitmap(width, height, stride, PixelFormat.Format32bppArgb, ptr);
}
Perhaps you can send me your solution files so that I can take a look at your code and possible fix it.
Mar 9, 2015 at 9:27 AM
Edited Mar 9, 2015 at 5:18 PM
Perfect thanks. I had created myself the first 2 sections / examples given.

What I was missing was the last bit to convert the ARGB into the bitmap to display on screen.
Many thanks for the example, I shall create the solution this morning and post

Cheers
Mar 9, 2015 at 11:36 AM
Next issue! Dynamics CRM doesn't like unsafe code..! The example shown has to be used like this:
                unsafe
                {
                    fixed (byte* p = rawARGBImageData)
                    {
                        IntPtr ptr = (IntPtr)p;
                        Bitmap newBitmap = new Bitmap(writer.Options.Width, writer.Options.Height, 4, PixelFormat.Format32bppArgb, ptr);
                    }
                }
I will try a work around to this, but you've pointed me in the right direction