Writing Bitmap to OpenXML ImagePart via MemoryStream - openxml

I have an image stored in a Bitmap object that I'd like to stick into an OpenXML document. I've tried using a MemoryStream as an intermediate step as follows:
ImagePart part = container.AddNewPart<ImagePart>("image/jpeg", imageId);
using (MemoryStream ms = new MemoryStream())
{
bitmap.Save(ms, ImageFormat.Jpeg);
part.FeedData(ms);
}
but that always results in empty files in the media folder and PowerPoint displaying an error instead of the images. I know that the MemoryStream has the image data correctly as I've written it out to a file without issue. When I try to load an image from a FileStream it works just fine.
How can I get this Bitmap into an OpenXML document?

I was almost there, I just needed to reset the MemoryStream's position to the beginning after saving the Bitmap to it.
ms.Position = 0;
That line should be added between the Save and FeedData calls.

Related

How load/save a number with saved games for Play Games Services in Unity games?

I need to load/save the number of coins a user has earned in my Unity game with saved games for Play Games Services.
There is an example on how to save an image on this page: https://developer.android.com/games/pgs/unity/saved-games#write_a_saved_game
Can someone tell me how I can load/save a number instead of an image?
To be precise, I wouldn't say you are exactly saving an image. I mean, you do save it, but only as a cover for your game save file and I don't know if you can retrieve it (maybe you can, I haven't checked that really).
Probably the most important part of using google play save system is byte[] savedData argument. It's just a byte array, and it's up to you what are you going to pass there and how are you going to interpret that data on game load.
There are a lot of ways you could approach it. I personally create a custom GameSave object with all my data that I want to save, then I serialize it using JsonUtility.
string json = JsonUtility.ToJson(gameSave);
After that, I use MemoryStream and BinaryFormatter to convert the data in my json to byte array:
MemoryStream memoryStream = new MemoryStream();
BinaryFormatter binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(memoryStream, json);
byte[] data = memoryStream.GetBuffer();
Then you would have to pass that data to savedGameClient.CommitUpdate method as an argument.
Of course, that's just one of the ways of doing that, you can send something else than json from serialized class object.
Other stuff is pretty well documented, so once you handle that, you should manage to do the rest.

How to generate grayscale bitmap image using HEX data in XML on MacOS (Swift)

I have image source in XML format (the image size is variable therefore I will need to change the image placeholder size). The image source is individual pixel data written in HEX format as string. It is grayscale BMP image data (without header).
I would like to know how to work with the data in order to be able to convert the text HEX pixel data into an image to be displayed in MacOS app using Xcode and Swift.
I have achieved similar thing years back using Action Script for AIR app.
The image source can look like:
<Signature t="8209">000200010000000203435A4B03000000C8010205410....
So first thing is to convert the text HEX string into actual binary data, in Action Script I have used this code (part of cycle to read the whole known length of image data):
var pixelData:uint = parseInt(imgdata.substr((j-1)*2,2),16);
In this example, part of the "signature" XML key is also information about the actual image size, so I would like to be able to change the image size based on that data and be safe not to over/under run the byte stream length needed for the actual image.
So the whole looping through the image pixel data and constructing byte array with alpha, red, green, blue values looked like:
var bx2:ByteArray = new ByteArray();
if(img2_w!=0){
for (var j:Number = 0; j <imgdata.length / 2; j++)
{
var pixelData:uint = parseInt(imgdata.substr((j-1)*2,2),16);
bx2.writeByte(255);
bx2.writeByte(pixelData);
bx2.writeByte(pixelData);
bx2.writeByte(pixelData);
}
}
then it was about linking the byte array image data to the image itself:
var b_image1:ByteArray = new ByteArray();
var bmd_image1:BitmapData = new BitmapData(img1_w,img1_h, false, 0xFFCC00);
//getting the parsed XML data off selected item in the displayed grid - P6Grid
b_image1 = sigData[P6Grid.selectedIndex].sig1;
b_image1.position=0;
bmd_image1.setPixels(bmd_image1.rect,b_image1);
//reference to the visual component placed on the UI
img_sn1.source = bmd_image1;
Would anyone know how to do this using Swift and Xcode on MacOS app. Currently I am using sotory boards and figured out how to handle the file drop, check if that is XML document and parse the key values, so I can get to the XML data, but don't know how to turn them into the image.
Many thanks for your thoughts,
JendaDH

Storing images in windows phone 8

I have been really cracking my head trying to write and read png files into a folder in Windows Phone 8. From few blogs sites and codeplex i found that the there is an extension to the WritableBitmap Class which provides few extra functionalities. ImageTools has PNG encoder and decoder. But I really cant find examples to use them.
What Im trying to achieve here is to create a folder called page and then in it a file called Ink File. I want to convert the bitmap to a PNG and store it there. The bitmap is created from the strokes drawn on a canvas. The class ImageTools provides a function called ToImage to convert the strokes from the canvas to image.
For storing
ExtendedImage myImage = InkCanvas.ToImage();
var encoder = new PngEncoder();
var dataFolder = await local.CreateFolderAsync("Page", CreationCollisionOption.OpenIfExists);
StorageFile Ink_File = await dataFolder.CreateFileAsync("InkFile", CreationCollisionOption.ReplaceExisting);
using (var stream = await Ink_File.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite))
{
using (var s = await Ink_File.OpenStreamForWriteAsync())
{
encoder.Encode(myImage, s);
await s.FlushAsync();
s.Close();
}
}
Is this a correct method? I receive some null exceptions for this. How do i find if the image is saved as png. How is this image saved? Is it encoded and saved in a file or is it saved as a png itsef. And how do we read this back?
I have checked out this, this , this and lot more like this.
I'm developing app for WP8
I have used the PNG Writer Library found in ToolStack and it works :)

Memory management using IText in Java

I am generating PDF's using IText and have noticed that the servers I am using seem to be running out of memory over time. I've turned off my feature to use IText and the servers seem fine - so I'm fairly certain there's something within my implementation of IText that is causing a memory leak. Here's my structure for IText:
Document document = new Document(PageSize.A4);
file = new File("/tmp/Hotel-Fax-" + voucher.getVoucherID() + ".pdf");
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
writer.setStrictImageSequence(true);
document.open();
PdfContentByte cb = writer.getDirectContent();
//Some methods here which just create PDFPTables and add some text / images
cb.stroke();
document.close();
return file;
I am wondering if there is some way that my PdfWriter or PdfContentByte are holding on to memory even though I've closed the document, meaning that next time the code is called, it just creates new objects and the memory eventually runs out?

How to Append PDF files in iOS SDK

I would like to append pdf files, i ll be getting NSData from server which i am saving as PDF, i want to add more data to existing PDF, i tried appending NSData which is appending the no.of bytes but if i save it as PDF then it only saves the appended part not the main part.
Example:
NSMutableData of existing PDF is firstdata
NSData of second PDF that i got from server is seconddata
now i have appended firstdata with seconddata and save it in a file this will only save the PDF part that is there in seconddata.
can anyone help me in solving this.