You are viewing limited content. For full access, please sign in.

Question

Question

Using the LaserficheImaging library

SDK
asked on January 20, 2015

Does anyone have sample code for using the LaserficheImaging .NET library directly? Specifically, I want to use it to resize an image. One of the instructors at Empower suggested that it might perform better than the built-in .NET methods in the System.Drawing.Imaging namespace.

0 0

Replies

replied on January 20, 2015

You can use code like this:

 

public byte[] ScaleDown(String file, int scale)
{
    using (var fileStream = File.OpenRead(file))
    {
        LfiBitmapSource orig = new LfiWriteableBitmap(fileStream);
        int newWidth = orig.PixelWidth * scale / 100, newHeight = orig.PixelHeight * scale / 100;
        var transformed = new LfiResizedBitmap(orig, newWidth, newHeight, LfiResizeOptions.AllowInterpolation);

        LfiBitmapFrame bitframe = new LfiBitmapFrame(transformed);
        LfiBitmapEncoder encode = new LfiBitmapEncoder(LfiContainerFormat.Png);
        encode.Frames = new List<LfiBitmapFrame>() { bitframe };
        using (var ms = new MemoryStream())
        {
            encode.Save(ms);
            return ms.ToArray();
        }
    }
}

The rest of the project from ACI351 will be posted to the support site soon.

3 0
replied on January 20, 2015 Show version history

Some more suggestions:

  1. All LfiBitmapSource classes implement IDisposable. Therefore, it is suggested to either put "orig", "transformed" within a using block, or to call Dispose before the function's end to ensure the memory is released in a timely manner. This is particularly important for long-running processes and batch processes.
  2. LfiBitmapEncoder.Frames is automatically initialized with an empty list. Therefore, it is safe to call "encode.Frames.Add(new LfiBitmapFrame(...))" without setting it to a new List.
  3. For TIFF image format, the image format needs to be specified in two pieces: firstly, the container format which is LfiContainerFormat.Tiff, secondly, for each image page (frame) the coded needs to be specified with a LfiBitmapCodec enum as well. Examples are: FaxGroup4 (for bitonal), TiffLzw, TiffFlate, (for lossless grayscale or color), and LfiBitmapCodec.Jpeg (for Tiff-Jpeg which is useful for multi-page lossy color compression).
  4. In case the code is to be deployed in a long-running, multi-threaded services (such as inside an IIS worker process with heavy traffic for more than 12 hours), prefer the classes in the "Laserfiche.Imaging.Core" namespace. The class names have prefix "Lf" as opposed to "Lfi", otherwise they are almost the same. The reason is that classes in "Laserfiche.Imaging" namespace are WPF compatible (Windows Presentation Foundation), but WPF compatibility is in conflict with server-side applications due to a handle leak issue. The non-WPF compatible classes in "Laserfiche.Imaging.Core" namespace do not have this handle leak issue. Therefore, the latter are recommended for heavy-duty server-side applications.
2 0
replied on January 20, 2015

Thanks, this works great. I am getting performance increases of 25% - 40% over the .NET System.Drawing.Image.GetThumbnailImage method.

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.