I’m trying to insert a user-provided picture (might be .png or .jpeg or whatever) as a header into a word document. Christian’s WordMBS class provides two ways of doing this, but I can’t get either of them to work.
Use WordFileMBS.addMediaFile(name as string, content as string)
This would mean I’d have to mess with the internal xml files, which I’ve tried to do without much success.
Insert a placeholder Image and replace its content via
// load image
Dim ImageFile As FolderItem = SpecialFolder.Desktop.Child(“new.jpg”)
Dim ImageStream As BinaryStream = BinaryStream.Open(ImageFile)
Dim ImageData As String = ImageStream.Read(ImageStream.Length)
word.MediaFile(name) = ImageData
(code is straight from Monkeybread Xojo plugin - WordFileMBS methods)
This works, but the problem is that the inserted picture will always be the same dimensions as the placeholder picture. And my placeholder is a .png, not sure what happens if I try to replace it with a .jpeg. Anyway, as I can’t anticipate what dimensions the user provided picture will have I can’t seem to go this way either. Any ideas?
Please, when sharing code, use the icons to tell the forum software the selected text is code ad in:
Click in this icon:
// load image
Dim ImageFile As FolderItem = SpecialFolder.Desktop.Child("new.jpg")
Dim ImageStream As BinaryStream = BinaryStream.Open(ImageFile)
Dim ImageData As String = ImageStream.Read(ImageStream.Length)
word.MediaFile(name) = ImageData
Understand the difference ?
And while I am at it, when you got an answer, click in the CheckBox, so readers will know.
Yeah, I was wondering about that too, but as I said, I just copied the code from Christians documentation. Anyway, this doesn’t solve the fixed picture size problem I mentioned.
Often with other languages special codes like 0 cause massive problems with strings. Images are pretty likely to contain null bytes. I’m aware that Xojo string do not use null terminated strings.
With UTF-8 being a “default” encoding for strings you also, in theory, are producing bad encoding values. Relying on “implementation details” seems like bad practice.
String is a bag o’ bytes, this is what I was taught and why Text datatype was a thing at one point.
String and MemoryBlock are interchangeable as far as I know, you can convert between them with an assignment (var s as String = mb || var mb as MemoryBlock = s)
Text was dropped for a reason. String is taking over from what Text was. You only have to look at String iterators to see that “Characters” are now something in strings. I’ve nothing against strings but they seem to be conceptually a bad fit for binary data.