The most efficient way to use a single Graphics.CopyFromScreen bitmap in multiple threads?

So, I have one stream that will constantly capture a screenshot and put the bitmap returned from Graphics.CopyFromScreeninto the public object of the bitmap. The idea is to use several other streams in this bitmap, but only for reading, without writing to the bitmap. Initially, I tried to just read it, but quickly found that it was blocking the entire bitmap so that it could not be used elsewhere. My second option was to simply create a new temporary bitmap in each stream that should use a public bitmap, and justtempBitMap = publicBitMap. This seems like a few exceptions, and I feel that is probably the best way anyway. I saw that you can block certain bits / pixels that need to be used in several streams, and then release them as soon as you finish with them, but does recording prevent the entire raster image from being locked for a while? Or it just writes to the entire bitmap, with the exception of the locked bits / pixels. Methods using a publicly available bitmap use only one pixel at a time, so this can probably work, provided that the public bitmap can be written while some of its bits / pixels are locked.

Just look for the most effective solution, if anyone can give advice on the most effective solution, I would be very grateful, thanks.

EDIT: I use the BitMap.GetPixel () method, which blocks the bitmap.

+4
source share
2 answers

My second alternative was to simply create a new temporary bitmap in each thread that should use the public bitmap, and just tempBitMap = publicBitMap

This is the right approach. You cannot write and read a bitmap at the same time. You should probably activate two bitmaps at the same time. One for writing, one for readers. After each entry, you change them. Please note that readers must be made with a bitmap before recording.

, , , , . , , GC .

, , , , , . .

, . , .

, /, , , , ?

. . , , , .

.

+1

, , . Clone , .

0

Source: https://habr.com/ru/post/1539162/


All Articles