Converting SVG image to TIFF


#1

i am trying to convert a svg image to tiff but after converting the image is not seen as whole some part of the image is visible
in my code i am converting svg image to stream and then using TiffOptions to convert, but the whole image is not visible after conversion


#2

@Sam212111

Thank you for contacting support.

Would you please share source and generated ZIP files along with SSCCE code so that we may try to reproduce and investigate it in our environment. Before sharing requested data, please ensure using latest version of the API.


#3

New Compressed (zipped) Folder.zip (2.8 KB)

i attached the svg img and the output tiff what i am getting, please share your code to get the same img in tiff format …


#4

@Sam212111

Thank you for sharing requested data.

You may use below code snippet to convert SVG to TIFF. Feel free to contact us if you need any further assistance.

// Instantiate Document object
Document doc = new Document();

// add page to pages collection of PDF file
Page page = doc.Pages.Add();

// Create an image instance
Aspose.Pdf.Image img = new Aspose.Pdf.Image();

// Set image type as SVG
img.FileType = (ImageFileType.Svg);

// Path for source file
img.File = (@"D:\636963846810645106.svg");

// Set left margin for image instance
img.Margin.Left = (10);

// Set Top margin for image instance
img.Margin.Top = (10);

// add image to PDF file
page.Paragraphs.Add(img);

doc.ProcessParagraphs();

// Create Resolution object
Resolution resolution = new Resolution(300);

// Create TiffSettings object
TiffSettings tiffSettings = new TiffSettings();
tiffSettings.Compression = CompressionType.None;
tiffSettings.Depth = ColorDepth.Default;
tiffSettings.SkipBlankPages = false;
// Create TIFF device
TiffDevice tiffDevice = new TiffDevice(resolution, tiffSettings);
tiffDevice.Process(doc, @"D:\SVGtoTIFF.tiff");

#5

the requirement is to convert svg img into stream and then using options to convert it into tiff img stream and then in end converting tiff img stream to pdf , what i was trying to do when got the output tiff image as sent.


#6

what i want to do is take the stream having my current document and the stream of tiff got and then forming the final pdf


#7

@Sam212111

Would you please elaborate your requirements a little more. Kindly specify if you want the final output to be a TIFF file or a PDF document. Moreover, above code snippet can easily be used with streams as well; you may read an image into stream and then set img.ImageStream instead of img.File. Likewise, tiffDevice.Process and doc.Save method accept streams instead of file paths.

In case you face any problem then feel free to contact us.


#8

try
{
using (var stream = new MemoryStream({BYTE[] OF IMAGE}))
{

                    using (Aspose.Imaging.FileFormats.Svg.SvgImage svgImg = (Aspose.Imaging.FileFormats.Svg.SvgImage)Aspose.Imaging.Image.Load(stream))
                    {
                        //svgImg.ResizeWidthProportionally(200);
						TiffOptions options = new TiffOptions(TiffExpectedFormat.Default);

                        using (var tiffStream = new MemoryStream())
                        {
                            
                                var svgFileName = fileName + ".svg";
                                
                                svgImg.Save(svgFileName); // THIS IMAGE WITH FILENAME.SVG IS COMING UP CORRECTLY

                            //tiffDevice.Process()
                            //svgImg.Save(tiffStream);
                            svgImg.Save(tiffStream, options);//AFTER THIS LINE THE IMAGE BYTE[] IS PLACED IN tiffStream USING OPTIONS 
                            
                            retBytes = tiffStream.ToArray(); //THIS retBytes is used then as below
                        }
                    }
                }
            }
			
			 using (var newstream = new MemoryStream(retBytes))
            {
                using (Aspose.Imaging.FileFormats.Tiff.TiffImage newTiff = (Aspose.Imaging.FileFormats.Tiff.TiffImage)Aspose.Imaging.Image.Load(newstream))
                {
                    using (var newtiffStream = new MemoryStream())
                    {
                        
                        
                        newTiff.BackgroundColor = Aspose.Imaging.Color.White;
                       
                            var tiffFileName = fileName + ".tiff";
                            newTiff.Save(tiffFileName);// THIS FILE IS OUTPUT TIFF FILE WHICH I ATTACHED EARLIER (INCORRECT)

                        newTiff.Save(newtiffStream);
                        retBytes = newtiffStream.ToArray(); //THE retBytes is then converted to pdf
                    }
                }
            }
            return retBytes;
        }

#9

Please refer it


#10

@Sam212111,

I suggest you to please try using following sample code an it worked fine on our end.

        try
        {
            String path = @"C:\data\";
            var data = File.ReadAllBytes(path + "636963846810645106.svg");
            using (var stream = new MemoryStream(data))

            {

                stream.Position = 0;
                using (Aspose.Imaging.FileFormats.Svg.SvgImage svgImg = (Aspose.Imaging.FileFormats.Svg.SvgImage)Aspose.Imaging.Image.Load(stream))
                {
                    //svgImg.ResizeWidthProportionally(200);
                    TiffOptions options = new TiffOptions(TiffExpectedFormat.Default);

                    using (var tiffStream = new MemoryStream())
                    {
                        var svgFileName = path + "Saved" + ".tiff";
                        svgImg.Save(svgFileName, options); // THIS IMAGE WITH FILENAME.SVG IS COMING UP CORRECTLY
                    }
                }
            }
        }
        catch(Exception ed)
        {
            String s = ed.StackTrace;
        }

Saved.zip (1.0 KB)