1

No doubt this isn't possible but i would like to see if anyone has an ingenious suggestion. We have a third party assembly which can output an image stored internally within a bespoke database to file using an internal method 'SaveToFile', an example:

3rdParty.Scripting.ImageManager man = new 3rdParty.Scripting.ImageManager("ref");

3rdParty.Scripting.Image itemImg = man.GetImage(orderNumber);

itemImg.SaveToFile("c:\file.jpg")

ItemImg.SaveToFile has no return type and just creates a bitmap internally and writes that to a filestream. We have absolutely no access to the compiled method.

What i need to do is somehow intercept the filestream and read the bitmap, i know this probably isn't possible but i'm no absolute expert so wanted to see if there is a magical way to do this.

If all else fails i'll save the file then read it back, i just want to avoid saving to disk where i might be able to obtain the data directly and eventually convert that to a base64 string value.

Dan Hall
  • 1,474
  • 2
  • 18
  • 43
  • Use a MemoryStream. – jdweng Sep 23 '16 at 16:26
  • 1
    `3rdParty.Scripting.Image itemImg = man.GetImage(orderNumber);` -- This is the image here in memory, is it not? Surely they give more access to the image than only being able to save it to file, e.g., if you have access to all the pixel data then can't you just construct the "base64 string value" from `itemImg`? – Quantic Sep 23 '16 at 16:28
  • Im with @Quantic on this. Do some debugging and see what the private members are for this `Image` type. If there is something loaded in managed memory you could use reflection to get that stream from the private members of that type. The code though will be very fragile and should be retested with any version changes made by that library. – Igor Sep 23 '16 at 16:32
  • Unfortunately that is just a constructor, there are no methods to return the stream...only to export to file :( – Dan Hall Sep 23 '16 at 16:57
  • Probably you're looking for something like [Can I get a path for a Memory Mapped File?](http://stackoverflow.com/q/1114786/18192). Unfortunately, the answer to that question is no. – Brian Nov 02 '16 at 20:46

1 Answers1

8

Unfortunately unless the 3rd party library provides a SaveToStream method where you could provide the stream from the outside there's no way to achieve what you are after. You will have to save the contents to a temporary file and then read the contents back.

That's why it's usually best practice when designing a library to provide methods taking Streams as I/O parameters as this would give the consumer the control of whether he wants to save it to a file, memory or network stream.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Thanks Darin, my thoughts exactly...it's a terrible library but we have no choice but to use it. Thanks. – Dan Hall Sep 23 '16 at 16:58