LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

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

Solved!
Go to solution
Highlighted

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
(4,078 Views)
Highlighted

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

0 Kudos
Message 2 of 10
(4,076 Views)
Highlighted
Solution
Accepted by pobrepablo1

MemoryStreams are one way to do it.

 

 

Message 3 of 10
(4,063 Views)
Highlighted

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
(4,059 Views)
Highlighted

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

0 Kudos
Message 5 of 10
(4,051 Views)
Highlighted

@Darin.K wrote:

MemoryStreams are one way to do it.

 

 


I have memory usage problem with your GDI Resize.vi 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
(1,515 Views)

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
(1,503 Views)
Highlighted

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
(1,482 Views)
Highlighted

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

/Y

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."
Certified-LabVIEW-Developer
0 Kudos
Message 9 of 10
(1,472 Views)
Highlighted

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
(1,441 Views)