Exception"You cannot modify the position of a FtpDataStream"while streaming PDF over FTP (C#)

Hi

The pseudo code below works fine but when I change JpegOptions from PNGOptions, I get an FTP error:

public void ConvertToJpgTestMethod()
{
	try
	{
		using (var ftpClient = new FtpClient
		{
			Host = "host",
			Credentials = new NetworkCredential(
				@"username",
				"password")
		})
		{
			var photoBytes = DownloadAssetThumb(api, asset, "Client");

			using (var pdfStream = new MemoryStream(photoBytes))
			{
				var page = new Document(pdfStream) { Background = Aspose.Pdf.Color.White }
					.Pages[1];

				using (var imageStream = new MemoryStream())
				{
					var destinationPath = @"/site/wwwroot/ftptest";
					var baseFileName = asset.WebName.Replace(" ", "-").ToLower();

					var standardFilePath = destinationPath + "/" + baseFileName + OutputFormat;

					using (var standardImageStream = ftpClient.OpenWrite(standardFilePath))
					{
						CustomTransformation(imageStream, standardImageStream, _standardSecondaryImageSize);

						imageStream.Position = 0;
						using (Aspose.Imaging.Image imageAspose = Aspose.Imaging.Image.Load(imageStream))
						{
							var rasterImg = CustomBackground(imageAspose);

							Aspose.Imaging.ImageOptions.JpegOptions jpgCreateOptions = new Aspose.Imaging.ImageOptions.JpegOptions();
							jpgCreateOptions.Source = new StreamSource(new MemoryStream(), true);
							var ImageB = Aspose.Imaging.Image.Create(jpgCreateOptions, _standardSecondaryImageSize.Width, _standardSecondaryImageSize.Height);

							//Aspose.Imaging.ImageOptions.PngOptions pngCreateOptions = new Aspose.Imaging.ImageOptions.PngOptions();
							//pngCreateOptions.Source = new StreamSource(new MemoryStream(), true);
							//var ImageB = Aspose.Imaging.Image.Create(pngCreateOptions, _standardSecondaryImageSize.Width, _standardSecondaryImageSize.Height);

							var graphics = new Graphics(ImageB);
							graphics.Clear(Aspose.Imaging.Color.White);
							graphics.DrawImage(rasterImg, ImageB.Bounds);

							ImageB.Save(standardImageStream);

						}
					}
				}
			}
		}
	}
	catch (Exception e)
	{
		Console.WriteLine(e);
	}

This is the error:
{System.InvalidOperationException: You cannot modify the position of a FtpDataStream. This property is updated as data is read or written to the stream.

Sorry, if that is a bit vague but can anybody help?
Thanks in Advance
Martin Smith

@icg.digital

Thank you for contacting support.

I would like to request you to share a narrowed down sample application reproducing this issue because this code sample includes few undefined methods and properties. Please also share a snapshot of the exception to specify source of the exception, so that we may proceed to help you out. Before you share requested data, please ensure using Aspose.PDF for .NET 18.2 and Aspose.Imaging for .NET 18.2 in your environment.

Thanks Farhan

I can confirm I am using the latest versions (18.2) of the library.
Here is a nicer working test method in which I have removed all the undefined methods.

    [TestMethod]
        public void ConvertToJpgTestMethod()
        {
            const string outputFormat = ".JPG";  //working
            //const string outputFormat = ".PNG";  //not working

            using (var ftpClient = new FtpClient
            {
                Host = "...ftp.azurewebsites.windows.net",
                Credentials = new NetworkCredential(
                    @"user",
                    "password")
            })
            {
                try
                {
                    byte[] photoBytes = File.ReadAllBytes(@"C:\Files\Images\01999-150dpi.pdf");

                    using (var pdfStream = new MemoryStream(photoBytes))
                    {
                        var page = new Document(pdfStream) {Background = Aspose.Pdf.Color.White}
                            .Pages[1];

                        var resolution = new Aspose.Pdf.Devices.Resolution(300); 

                        using (var imageStream = new MemoryStream())
                        {
                            ImageOptionsBase options;

                            if (outputFormat.ToUpper() == ".PNG")
                            {
                                var pngDevice = new PngDevice(resolution);
                                pngDevice.Process(page, imageStream);
                                options = new Aspose.Imaging.ImageOptions.PngOptions();
                            }
                            else
                            {
                                var jpgDevice = new JpegDevice(resolution);
                                jpgDevice.Process(page, imageStream);
                                options = new Aspose.Imaging.ImageOptions.JpegOptions();
                            }

                            var testFilePath = @"/site/wwwroot/ftptest/test" + outputFormat;

                            using (var standardImageStream = ftpClient.OpenWrite(testFilePath))
                            {
                                imageStream.Position = 0;
                                using (Aspose.Imaging.Image imageAspose = Aspose.Imaging.Image.Load(imageStream))
                                {
                                    var rasterImg = imageAspose as RasterImage;

                                    options.Source = new StreamSource(new MemoryStream(), true);

                                    var size = new Aspose.Imaging.Size(1200, 800);
                                    var imageB = Aspose.Imaging.Image.Create(options, size.Width, size.Height);

                                    var graphics = new Graphics(imageB);
                                    graphics.Clear(Aspose.Imaging.Color.White);
                                    graphics.DrawImage(rasterImg, imageB.Bounds);

                                    imageB.Save(standardImageStream);

                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex);
                }
            }
        }

This is the stack trace:

System.InvalidOperationException: You cannot modify the position of a FtpDataStream. This property is updated as data is read or written to the stream.
at System.Net.FtpClient.FtpDataStream.set_Position(Int64 value)
at .set_Position(Int64 )
at .Write(Byte[] , Int32 , Int32 )
at .(Stream , RasterImage , PngOptions , Rectangle , Boolean , String )
at Aspose.Imaging.FileFormats.Png.PngImage.SaveData(Stream stream)
at Aspose.Imaging.DataStreamSupporter.Save(Stream stream)
at …UnitTests.cs:line 218

@icg.digital

We will appreciate if you can share a zip file containing a sample application reproducing this issue, along with the source file(s) that you are using on your side. Shared stack trace clarifies that the issue pertains to Aspose.Imaging API so this thread has been moved to Aspose.Imaging forum. Once you share requested data with us, my colleague from respective team will take care of your concerns.

@icg.digital,

I have worked with sample code provided by you and have not been able to use that completely on my end owing to custom calls. However, I have used following sample code on my end to verify the example based on Aspose.PDF and Aspose.Imaging. There is no issue as far as APIs are concerned and it worked on my end using following modified code with out any issue.

public static void TestPdfIssue()
{

    string outputFormat = ".PNG";
    try
    {
        String path = @"C:\Users\Muhammad\Downloads\PdfConversionUnitTestProject\PdfConversionUnitTestProject\";
        byte[] photoBytes = File.ReadAllBytes(path+"01999-150dpi.pdf");  //teh path may need to be changed

        using (var pdfStream = new MemoryStream(photoBytes))
        {
            var page = new Aspose.Pdf.Document(pdfStream) { Background = Aspose.Pdf.Color.White }
                .Pages[1];

            var resolution = new Aspose.Pdf.Devices.Resolution(300);

            using (var imageStream = new MemoryStream())
            {
                ImageOptionsBase options;

                if (outputFormat.ToUpper() == ".PNG")
                {
                    var pngDevice = new PngDevice(resolution);
                    pngDevice.Process(page, imageStream);
                    options = new Aspose.Imaging.ImageOptions.PngOptions();
                }
                else
                {
                    var jpgDevice = new JpegDevice(resolution);
                    jpgDevice.Process(page, imageStream);
                    options = new Aspose.Imaging.ImageOptions.JpegOptions();
                }

                var testFilePath = path+"test" + outputFormat;
                FileStream file = new FileStream(testFilePath, FileMode.Create, FileAccess.Write);
                imageStream.WriteTo(file);
                file.Close();
                imageStream.Close();
                //using (var standardImageStream = ftpClient.OpenWrite(testFilePath))
                {
                    //imageStream.Position = 0;
                    using (Aspose.Imaging.Image imageAspose = Aspose.Imaging.Image.Load(testFilePath))
                    {
                        var rasterImg = imageAspose as RasterImage;

                        options.Source = new StreamSource(new MemoryStream(), true);

                        var size = new Aspose.Imaging.Size(1200, 800);
                        var imageB = Aspose.Imaging.Image.Create(options, size.Width, size.Height);

                        var graphics = new Graphics(imageB);
                        graphics.Clear(Aspose.Imaging.Color.White);
                        graphics.DrawImage(rasterImg, imageB.Bounds);

                        // imageB.Save(standardImageStream);
                        imageB.Save(path + "SavedImage" + outputFormat);
                    }
                }
            }
        }
    }
    catch (Exception ex)
    {
        Debug.WriteLine(ex);
        // Assert.Fail(ex.Message);
    }
}

For your kind reference, I have attached the generated output files as well.

Data.zip (1.2 MB)

Thanks for your response Mudassir but the issue was not that it cannot convert to PNG at all, the issue is that it fails to convert to PNG when uploading to a FTP site. See the code block from my second post for a better example.

@icg.digital,

Can you please provide sample Visual Studio Project to us that we may use on our end with all dependencies. We will use that to investigate the issue further on my end.

I have attached a test project
AsposeIssue172401TestProject.zip (3.7 KB)
Thanks

@icg.digital,

I have used the CS file shared by you and have created the solution based on that. However, I am unable to use the solution as I have no access to FTP server probably. On my end, both JPEG and PNG methods fail with exception on loading files. Please update the attached solution and as requested earlier please provide a working sample project that we may use on our end to reproduce the issue and help you.

TestImagingFTP.zip (761.7 KB)

Hi Mudassir
I cannot provide you access to our FTP Server, can you set up your own server to test with? Preferably on Azure as that would be the most valid test.

@icg.digital,

I regret to share that I don’t possess Azure account. I request for sample test account that I may use to try reproducing the issue on my end to further investigate the issue. It would be preferable if you provide test account of your FTP server that our product team may also use as well if the issue gets reproduced on our end.

Would you be able to test it on another FTP server, then? You may be able to replicate the the issue.

@icg.digital,

I have observed your comments. I regret to share to share that we don’t have access to FTP servers and need a test account to reproduce the issue on our end.