We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

SetHeaderPicture error

Please could you help me?

I have this code but there is an error when i call :
pageSetup.SetHeaderPicture(2, binaryData);

Could you telle me why it doesn’t work?
My code :
var binaryData = new Byte[versicherungRow.Logo.Length];
var stream = new MemoryStream(binaryData);
//Reads a block of bytes from the stream and writes data in a given buffer of byte array.
long bytesRead = stream.Read(binaryData, 0, binaryData.Length);
//Setting the script for the logo/picture
pageSetup.SetHeader(2, “&G”);
pageSetup.SetHeaderPicture(2, binaryData);

Hi,


I have tested your scenario/ case a bit using the following sample code and it works fine. I am using latest version of the product. I have also attached the header image file and output Excel file for your reference.
e.g
Sample code:

//Instantiating a Workbook object
Workbook workbook = new Workbook();
//Get First Worksheet of the Workbook
Worksheet ws = workbook.Worksheets[0];

ws.Cells[“A9”].PutValue(“Testin…”);
//Creating a string variable to store the url of the logo/picture
string logo_url = “e:\test\school.jpg”;
//Declaring a FileStream object
FileStream inFile;
//Declaring a byte array
byte[] binaryData;
//Creating the instance of the FileStream object to open the logo/picture in the stream
inFile = new System.IO.FileStream(logo_url, System.IO.FileMode.Open, System.IO.FileAccess.Read);
//Instantiating the byte array of FileStream object’s size
binaryData = new Byte[inFile.Length];
//Reads a block of bytes from the stream and writes data in a given buffer of byte array.
long bytesRead = inFile.Read(binaryData, 0, (int)inFile.Length);
//Creating a PageSetup object to get the page settings of the first worksheet of the workbook
PageSetup pageSetup = workbook.Worksheets[0].PageSetup;

//Setting the logo/picture in the central section of the page header
pageSetup.SetHeaderPicture(0, binaryData);
//Setting the script for the logo/picture
pageSetup.SetHeader(0, “&G”);
//Setting the Sheet’s name in the right section of the page header with the script
pageSetup.SetHeader(2, “&A”);




//Save the workbook
workbook.Save(“e:\test2\outsetheaderpict1.xlsx”);

Could you try the above code with your header image (please set the path for the image file and output Excel file accordingly). If you still find the issue with latest version, kindly attached your image file here, we will check it soon.

Thank you.


Hi Amhad,


Thank’s for your answer … I have understand the example… but ma problem isn’t that.
i don’t want a FileStream I have the picture like this : // Logo
	Row.Logo = Row.Adress.Logo.GetImageAsByteArray();
and after take this logo … I don’t want an IO File with an Url.
Is possible?

Hi,


I used another code segment getting picture as byte array, it works also fine.
e.g
Sample code:

//Instantiating a Workbook object
Workbook workbook = new Workbook();
//Get First Worksheet of the Workbook
Worksheet ws = workbook.Worksheets[0];

ws.Cells[“A9”].PutValue(“Testin…”);
byte[] binaryData = File.ReadAllBytes(@“e:\test\school.jpg”);
//Creating a PageSetup object to get the page settings of the first worksheet of the workbook
PageSetup pageSetup = workbook.Worksheets[0].PageSetup;

//Setting the logo/picture in the central section of the page header
pageSetup.SetHeaderPicture(0, binaryData);
//Setting the script for the logo/picture
pageSetup.SetHeader(0, “&G”);
//Setting the Sheet’s name in the right section of the page header with the script
pageSetup.SetHeader(2, “&A”);




//Save the workbook
workbook.Save(“e:\test2\outsetheaderpict1.xlsx”);

I am not sure about your codes, I think the problem lies in your own code. Please make sure that you are getting the picture data into byte array fine, you should debug your code segment if you are getting the binary data ok or not, so you could figure it out on your end too.

Thank you.

Hi,


thank you for your quickly answer but i’m still having this Problem…
I debug the source code and i have a byte array… that’s not a Problem…

The problem is I don’t want this :
byte[] binaryData = File.ReadAllBytes(@“e:\test\school.jpg”);

I don’t want a file to give… I have already an object with the logo :
byte[] logo = Logo.GetImageAsByteArray();

The Problem is there :
var binaryData = new Byte[versicherungRow.Logo.Length];
pageSetup.SetHeader(2, “&G”);
pageSetup.SetHeaderPicture(2, binaryData);
thank you

Hi,


Could you create a sample console application (runnable) with v8.2.0 (latest), zip it and post us here to reproduce the issue on our end. Also attach the template files (image file, Excel files etc.), we will check your issue soon.

Thank you.

hi,


Sorry I found a solution Thank you… :wink:

Best regards

Julie

Hi,


Good to know that you have figured it out now. Feel free to contact us any time if you need further help or have some other issue or queries, we will be happy to assist you soon.

Thank you.

Hi,


Do you know how can i scale or fit the picture ?

pageSetup.SetHeaderPicture(2, versicherungRow.Logo).Height = 1;
ageSetup.SetHeaderPicture(2, versicherungRow.Logo).WidthCM = 0.2;

I have that but it doesn’t work

Thank you

Hi,


Please refer to the following lines of code segment and update/ replace your codes accordingly. I have tested it and it works fine, it changes the size of the inserted header picture accordingly.
e.g
Sample code:

//Creating a PageSetup object to get the page settings of the first worksheet of the workbook
PageSetup pageSetup = workbook.Worksheets[0].PageSetup;
//Setting the logo/picture in the central section of the page header
pageSetup.SetHeaderPicture(2, binaryData);
//Setting the script for the logo/picture
pageSetup.SetHeader(2, “&G”);

Picture pic = pageSetup.GetPicture(true,2);
pic.Height = intHeight;
pic.WidthCM = dblWidth;

//Saving the workbook
workbook.Save(“e:\test2\out1.xlsx”);

Thank you.