3

I am using Lead Tool with c#. And Got error in below code. I pass this string base64String value from JS when I cropped Image and then convert it in c# in to Image with Base64ToImage function. So this is all complete code which I did.

private static Image Base64ToImage(string base64String)
    {
        Image img = null;
        // Convert Base64 String to byte[]
        byte[] imageBytes = Convert.FromBase64String(base64String);
        using (MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length))
        {

            // Convert byte[] to Image
            ms.Write(imageBytes, 0, imageBytes.Length);
            img = System.Drawing.Image.FromStream(ms);
        }
        return img;
    }


public static void CropImage(string base64String)
    {
        Image img = Base64ToImage(base64String);
        using (MemoryStream ms = new MemoryStream())
        {
            img.Save(ms, ImageFormat.Bmp);
            ms.Seek(0, System.IO.SeekOrigin.Begin);
            using (RasterCodecs codecs = new RasterCodecs())
            {
                // Load the source image from disk
                using (RasterImage image = codecs.Load(ms))
                {
                    // Crop 100 pixels from each side of the image
                    CropCommand command = new CropCommand();
                    command.Rectangle = new LeadRect(
                       left,
                       top,
                       width,
                       height);
                    command.Run(image); 
                    // Save it to disk
                    codecs.Save(image, output, RasterImageFormat.Bmp, 24);
                }
            } 
        }
    }

An unhandled exception of type 'Leadtools.RasterException' occurred in Leadtools.Codecs.dll

Anyone please give my some solution for this.

LEADTOOLS Support
  • 2,755
  • 1
  • 12
  • 12
Mohit Saxena
  • 1,439
  • 1
  • 12
  • 21

1 Answers1

4

With LEADTOOLS 19, a license (eval or release) must be specified with the application before any LEADTOOLS features are used. If you haven't provided one, this is why you're getting the "Kernel has expired" message. If you have provided a license, check to see if it's still valid. If not, please contact the LEADTOOLS Sales team to get a valid license.

I couldn't get your code to work exactly as-is because I don't know how your Base64ToImage() method is returning an Image. In lieu of this, I took a more direct approach, and just loaded a file from disk to memory. This loads without any problem.

   class Program
   {
      static void Main(string[] args)
      {
         RasterSupport.SetLicense(@"C:\LEADTOOLS 19\Common\License\LEADTOOLS.LIC",
                                 File.ReadAllText(@"C:\LEADTOOLS 19\Common\License\LEADTOOLS.LIC.KEY"));

         Byte[] imageData = File.ReadAllBytes(@"C:\Users\Public\Documents\LEADTOOLS Images\cannon.jpg");

         using (MemoryStream ms = new MemoryStream(imageData))
         {
            // Put the pointer back to the beginning
            ms.Seek(0, System.IO.SeekOrigin.Begin);

            using( RasterCodecs codecs = new RasterCodecs())
            {
               // Load the source image from disk
               using (RasterImage image = codecs.Load(ms))  // on this line I got error...
               {
                  //do something with the image
               }
            }
         }
      }
   }

Since this works, it's possible the problem is within how you're creating the memory stream or what is in the memory stream. I recommend using the File.WriteAllBytes() method after creating your memory stream, and then read the file from disk. If this works, then the issue is in reading the memory stream. Typically this would mean the MemoryStream's position is not at the beginning. The code you have accounts for that though, so it's likely an issue with the data in the memory stream.

LEADTOOLS Support
  • 2,755
  • 1
  • 12
  • 12
  • Thanks for your reply. I have edited my code and post complete code which I did and base64String value I am passing from JS when I cropped Image. So have a look and suggest me. – Mohit Saxena Aug 25 '15 at 11:53
  • 1
    Mohit, Two things: 1.) I don't see from your updated code where you set a license for the SDK. As I noted previously, this must be done with LEADTOOLS 19. Any other LEADTOOLS call would fail with this exception if an eval or release license is not properly specified. Also, have you checked to make sure the license files that you have has not expired? The evaluation period is limited. 2.) Your code seems overly complicated for why it's doing. Why are you using the System.Drawing.Image to write a BMP file? Why not use RasterCodecs.Load() with the original memory stream you're creating? – LEADTOOLS Support Aug 27 '15 at 20:44
  • 1
    Why not like this? public static void CropImage(string b64str) { byte[] b = Convert.FromBase64String(b64str); using (MemoryStream ms = new MemoryStream(b, 0, b.Length)) { ms.Write(b, 0, b.Length); ms.Seek(0, System.IO.SeekOrigin.Begin); using (RasterCodecs codecs = new RasterCodecs()) { using (RasterImage img = codecs.Load(ms)) { CropCommand command = new CropCommand(); command.Rectangle = new LeadRect(l,t,w,h); command.Run(img); codecs.Save(img, fOut, RasterImageFormat.Bmp, 24); } } } } – LEADTOOLS Support Aug 27 '15 at 20:55
  • Thanks for your response. I checked it when I did RasterSupport. It gives me following data -------- Leadtools.RasterSupport base: object ConfigLicenseStatus: Failure KernelExpired: true KernelExpireDate: {8/12/2015 12:00:00 AM} KernelType: Evaluation ------ So how to renew this? – Mohit Saxena Sep 16 '15 at 10:41
  • If you purchased the toolkit and already have a LIC file and a KEY files, send them both to support@leadtools.com and explain the problem. If you're using the free evaluation, or you purchased the toolkit but did not receive a LIC/KEY file pair, contact sales@leadtools.com – LEADTOOLS Support Nov 22 '15 at 20:36
  • This problem has been resolved with license file updated. – Mohit Saxena Nov 23 '15 at 18:07