我在c#中使用Lead Tool。并得到错误在下面的代码。当我裁剪图像时,我从JS传递这个字符串base64String值,然后用Base64ToImage函数将其转换为c#中的图像。这是我写的完整代码
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);
}
}
}
}
类型为"Leadtools"的未处理异常。RasterException'发生在Leadtools.Codecs.dll
谁能给我一些解决办法呢
对于LEADTOOLS 19,在使用任何LEADTOOLS特性之前,必须与应用程序一起指定许可证(eval或release)。如果您没有提供一个,这就是为什么您得到"内核已过期"消息的原因。如果您提供了许可证,请检查它是否仍然有效。如果没有,请联系LEADTOOLS销售团队获取有效的许可证。
我不能让你的代码完全工作,因为我不知道你的Base64ToImage()方法是如何返回一个图像。为此,我采用了一种更直接的方法,将一个文件从磁盘加载到内存中。加载没有任何问题。
class Program
{
static void Main(string[] args)
{
RasterSupport.SetLicense(@"C:LEADTOOLS 19CommonLicenseLEADTOOLS.LIC",
File.ReadAllText(@"C:LEADTOOLS 19CommonLicenseLEADTOOLS.LIC.KEY"));
Byte[] imageData = File.ReadAllBytes(@"C:UsersPublicDocumentsLEADTOOLS Imagescannon.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
}
}
}
}
}
既然这工作,这是可能的问题是在你如何创建内存流或什么是在内存流。我建议在创建内存流之后使用file . writeallbytes()方法,然后从磁盘读取文件。如果这个工作,那么问题是在读取内存流。这通常意味着MemoryStream的位置不在开始。代码你有帐户,虽然,所以它可能是一个问题与内存流中的数据。