Showing results for 
Search instead for 
Did you mean: 

.Net image resize - how to convert to format labview can use?

Go to solution

Hji All, 

See the attached VI. I'm using some .Net code to resize an image, lacking the $5k for IMAQ. 


It instantaneously writes to a picturebox, but because I'm not using the Paint event it goes away when it is redrawn. So when you run it, it will appear to flicker. That's fine; I don't care about rendering to a Picturebox, I jsut want the image after it's been resized.


My question is, after I call DrawImage, how do I get that image into some format labview can use? Saving to a file & loading it is not an option. I tried pasting it to the clipboard using ClipBoard.SetImage but that results in an error: 


Inner Exception: System.Threading.ThreadStateException: Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it.)


I have no idea what this means. 


Any help is greatly appreciated! 

0 Kudos
Message 1 of 10

Please ignore the silly error in my VI. Forgot to enable a subdiagram in diagram disable structure. 

0 Kudos
Message 2 of 10
Accepted by pobrepablo1

MemoryStreams are one way to do it.



Message 3 of 10

Looks like a better way to rescale in .NET is to use the ScaleTransform method, instead of Draw.  (EDIT: never mind, I misunderstood what ScaleTransform does)


As for the error with the clipboard, try setting the VI to run in the User Interface execution subsystem, which forces it to run in a single thread.  This often resolves threading problems with .NET and ActiveX components.

Message 4 of 10

Thanks guys. Both good answers, and I've got it working now. 

0 Kudos
Message 5 of 10

@Darin.K wrote:

MemoryStreams are one way to do it.



I have memory usage problem with your GDI 22KB

I call these vi from main vi and after every call memory usage increase for ~50MB (which could be uncompressed size of my 4032 x 3024 jpeg with 4 bytes for pixel). Sub vi is not reentrant.

Because I want to resize around 100 pictures these is a problem.

0 Kudos
Message 6 of 10

My bad.  Dispose of the Bitmap object created from the path control and the Graphics object which is output from the FromImage terminal of the constructor.  Use the error connection so that the Dispose method is called at the end (at the same time the MemoryStream is disposed of).  I rip these examples from other places and sometimes miss an important piece.

0 Kudos
Message 7 of 10

Thanks. It's much better, but in the case of 50 pictures 4032 x 3024, 1GB of RAM usage is still being collected. With Resize off there is no Memory usage rise.

0 Kudos
Message 8 of 10

It sounds like you're allocating new memory for each picture, is there some wire/input to reuse buffers?


G# - Award winning reference based OOP for LV, for free! ADDQ VIPM Now on GitHub
"Only dead fish swim downstream" - "My life for Kudos!" - "Dumb people repeat old mistakes - smart ones create new ones."
0 Kudos
Message 9 of 10

I do not understand you. I thing that vi is clear. If resize is not performing, there is no memory problem.

0 Kudos
Message 10 of 10