Welcome Guest! To enable all features, please Login or Register.

Notification

Icon
Error

Options
View
Last Go to last post Unread Go to first unread post
#1 Posted : Wednesday, September 28, 2016 8:07:39 AM(UTC)

BWill  
BWill

Groups: Registered
Posts: 6


I'm working with the console application provided by Walter linked from the white paper Extend Existing Applications with the LEADTOOLS Virtual Printer SDK. It creates 3 image formats - a .emf, a .pdf, and a .png. I need a tiff file as well so I copied the method for .png and made what I thought were necessary changes, but it ends with an error at the line metaFile = new Metafile(e.Stream) below in blue

{System.Runtime.InteropServices.ExternalException (0x80004005): A generic error occurred in GDI+.
at System.Drawing.Imaging.Metafile..ctor(Stream stream)
at Virtual_Printer_Driver_Sample.EmfEventMethods.FillSection_Tif(Object sender, EmfEventArgs e) in c:\Users\bwilliams\Desktop\LeadTools Eval License\Virtual Printer Driver Console\Virtual Printer Driver Sample\EmfEventMethods.cs:line 130}

Here is the method. what do I need to do to get the code working?

[EmfEventMethodAttribute]
public static void FillSection_Tif(object sender, Leadtools.Printer.EmfEventArgs e)
{
ConsoleMethods.Info("EMF Event: FillSection_Tif");
const string format = "tif";
string path = Path.Combine(
GetOutputRootPath(),
format + @"\",
GetRandomFileName(format));
Directory.CreateDirectory(Path.GetDirectoryName(path));
// Get the EMF in memory and save as TIF.
Metafile metaFile = null;
try
{
metaFile = new Metafile(e.Stream);
IntPtr hEmf = metaFile.GetHenhmetafile();
using (RasterImage image = RasterImageConverter.FromEmf(hEmf, 0, 0, RasterColor.White))
{
using (RasterRegion region = new RasterRegion(new LeadRect(20, 30, 100, 200)))
{
image.SetRegion(null, region, RasterRegionCombineMode.Set);
new FillCommand(RasterColor.FromKnownColor(RasterKnownColor.Black)).Run(image);
}
using (RasterCodecs codecs = new RasterCodecs())
{
codecs.Save(image, path, RasterImageFormat.Tif, 1);
}
}
ConsoleMethods.Success("FillSection_Png: TIF saved. ");
ConsoleMethods.Verbose(path);
}
catch (Exception ex)
{
ConsoleMethods.Error(ex.Message, 4000);
}
}
}

Edited by moderator Friday, October 7, 2016 4:42:21 PM(UTC)  | Reason: added link to whitepaper

 

Try the latest version of LEADTOOLS for free for 60 days by downloading the evaluation: https://www.leadtools.com/downloads

Wanna join the discussion? Login to your LEADTOOLS Support accountor Register a new forum account.

#2 Posted : Thursday, September 29, 2016 4:58:11 PM(UTC)

Walter  
Walter

Groups: Tech Support, Administrators
Posts: 368

Thanks: 1 times
Was thanked: 4 time(s) in 4 post(s)

Hello,

I think the problem you're having is the memory stream position is being incremented to the end of the file by the prior conversions. I first tried removing the other conversions and creating the TIFF file worked. Then I saw the problem you noted with the EMF file in memory. After setting the position back to 0 immediately before using the stream, the conversion completed successfully for me. So all I had to do was add the following line:

// Get the EMF in memory and save as TIF.
Metafile metaFile = null;
try
{
e.Stream.Position = 0;
metaFile = new Metafile(e.Stream);

I'm attaching the updated 2010 project here for reference.

File Attachment(s):
Virtual Printer Driver Sample.zip (28kb) downloaded 49 time(s).
Walter Bates
Senior Support Engineer
LEAD Technologies, Inc.
LEAD Logo
 
#3 Posted : Monday, October 3, 2016 12:42:19 PM(UTC)

BWill  
BWill

Groups: Registered
Posts: 6


Thanks Walter - got it working.
 
#4 Posted : Monday, October 3, 2016 2:33:06 PM(UTC)

BWill  
BWill

Groups: Registered
Posts: 6


Quick question - why don't you have to rewind the stream after saving the EMF stream directly, creating the PDF and PNG?
 
#5 Posted : Monday, October 3, 2016 3:19:44 PM(UTC)

Walter  
Walter

Groups: Tech Support, Administrators
Posts: 368

Thanks: 1 times
Was thanked: 4 time(s) in 4 post(s)

That I'm not really sure of. If they're sharing the MemoryStream like they seem to be, then I would also expect you'd need to set the position each time to ensure it always starts at the beginning, or at least for each subsequent time after the first. Why it works a couple of times and then fails is a mystery to me as well.
Walter Bates
Senior Support Engineer
LEAD Technologies, Inc.
LEAD Logo
 
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Powered by YAF.NET | YAF.NET © 2003-2019, Yet Another Forum.NET
This page was generated in 0.110 seconds.