Author |
Message |
VbPro #1 / 25
|
 Snapshot of screen
Hi VB lovers, I'm using the following code to get a snapshot of the screen in a StdPicture object. http://www.*-*-*.com/ However my problem is I need to get the space used (no of bytes) by the image in memory. Is this possible? If not I would like to convert the image into a JPG file and then get the size of file in bytes. Size itself is not required actually. I just want to compare two images and see if they are the same. The only way I can think of is to compare there size/space in memory or disk. If there is another efficient way to do this please advise. Thanks
|
Tue, 08 May 2012 10:04:45 GMT |
|
 |
Dee Earle #2 / 25
|
 Snapshot of screen
Quote: > Hi VB lovers, > I'm using the following code to get a snapshot of the screen in a > StdPicture object. > http://www.devx.com/vb2themax/Tip/19172 > However my problem is I need to get the space used (no of bytes) by the > image in memory. Is this possible? If not I would like to convert the > image into a JPG file and then get the size of file in bytes. > Size itself is not required actually. I just want to compare two images > and see if they are the same. The only way I can think of is to compare > there size/space in memory or disk. If there is another efficient way to > do this please advise.
No, you can not relay on size to determine if they are the same. Nor can you rely on different (non bitmap) binary data to say they are different. Bitmaps in memory are a fixed (height x width x bytes per pixel) + header (with a few caveats for odd sizes and bit depths) Once compressed, the final image data may contain other data so you are unlikely to get exactly the same data for multiple encodes of the same image. The only way to reliably tell is to do a pixel by pixel comparison. If the images will have been subject to lossy compression, you will have to have a tolerance in there as well. What are you actually trying to achieve by seeing if the screen has changed? --
i-Catcher Development Team iCode Systems
|
Tue, 08 May 2012 17:23:17 GMT |
|
 |
Dave O #3 / 25
|
 Snapshot of screen
Quote: > The only way to reliably tell is to do a pixel by pixel comparison. > If the images will have been subject to lossy compression, you will have > to have a tolerance in there as well.
A while back I needed to compare bitmaps quickly and found that if I combined 2 bitmaps using XOR (probably*) then if the pictures were identical the result was pure black, then all I had to do was to zip through the pixels of a single image looking for any non-zero value. If the image is large you can save a bit of time by taking one half of the result and XOR it with the other half, then repeat until down to a few thousand pixels, again it will be all black if there were no non-black pixels. Trial & error is probably the only way to find the optimum for this. Of course if the images were lossy such as JPEGs then it does get a lot trickier as you'll get near black values, but that might be adequate to show close similarity. Another possibility that is best if you need to compare an image with several others is to take the CRC of the images. * Not sure it was XOR, could have been any logical operation and I don't have the code to hand. Dave O.
|
Tue, 08 May 2012 21:45:27 GMT |
|
 |
Nobod #4 / 25
|
 Snapshot of screen
See this post, which gets the contents of a PictureBox as byte array. The PictureBox AutoRedraw must be True. I believe you can adapt it to what you need. I have made another that works regardless AutoRedraw property, but it's few percentage points slower. That one uses BitBlt to copy PictureBox contents to the compatible DC, instead of using the PictureBox autoredarw bitmap itself. http://groups.google.com/group/microsoft.public.vb.general.discussion...
|
Tue, 08 May 2012 22:40:07 GMT |
|
 |
Larry Serflate #5 / 25
|
 Snapshot of screen
Quote: > A while back I needed to compare bitmaps quickly and found that if I > combined 2 bitmaps using XOR (probably*) then if the pictures were identical > the result was pure black, then all I had to do was to zip through the > pixels of a single image looking for any non-zero value. > If the image is large you can save a bit of time by taking one half of the > result and XOR it with the other half, then repeat until down to a few > thousand pixels, again it will be all black if there were no non-black > pixels. Trial & error is probably the only way to find the optimum for > this. > Of course if the images were lossy such as JPEGs then it does get a lot > trickier as you'll get near black values, but that might be adequate to show > close similarity. > Another possibility that is best if you need to compare an image with > several others is to take the CRC of the images. > * Not sure it was XOR, could have been any logical operation and I don't > have the code to hand.
I seem to recall a similar discussion... http://groups.google.com/group/microsoft.public.vb.general.discussion... "A while back" ? How about 8 years! <g> LFS
|
Wed, 09 May 2012 03:45:33 GMT |
|
 |
Martin Trum #6 / 25
|
 Snapshot of screen
Quote: > I'm using the following code to get a snapshot of the screen in a > StdPicture object. > http://www.devx.com/vb2themax/Tip/19172
Wow! What a chunk of code! I think this fragment I have used could be adapted to your needs. Private Declare Sub keybd_event Lib "user32.dll" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long) AppActivate getaviname keybd_event vbKeyMenu, 0, 0, 0 ' Press ALT keybd_event vbKeySnapshot, 0, 0, 0 ' Press Prntscr keybd_event vbKeySnapshot, 0, 2, 0 ' Release Prntscr keybd_event vbKeyMenu, 0, 2, 0 'Release ALT pic2.Picture = Clipboard.GetData() etc. then save the picture Quote: > However my problem is I need to get the space used (no of bytes) by the > image in memory. Is this possible? If not I would like to convert the > image into a JPG file and then get the size of file in bytes.
Comparing sizes of JPG's might be risky unless you know the level of compression of each is the same. HTH
|
Thu, 10 May 2012 20:56:54 GMT |
|
 |
Nobod #7 / 25
|
 Snapshot of screen
Quote: >> http://www.devx.com/vb2themax/Tip/19172 > Wow! What a chunk of code! > I think this fragment I have used could be adapted to your needs. > Private Declare Sub keybd_event Lib "user32.dll" (ByVal bVk As Byte, ByVal > bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long) > AppActivate getaviname > keybd_event vbKeyMenu, 0, 0, 0 ' Press ALT > keybd_event vbKeySnapshot, 0, 0, 0 ' Press Prntscr > keybd_event vbKeySnapshot, 0, 2, 0 ' Release Prntscr > keybd_event vbKeyMenu, 0, 2, 0 'Release ALT > pic2.Picture = Clipboard.GetData() > etc. > then save the picture
I could think of three issues with the code you posted: 1 - It would erase the clipboard, and this may annoy the user. 2 - The code you posted would capture the active window only, while the BitBlt method would capture any window unless it's obscured by other windows, in which case it captures the area in the desktop that it would usually occupy. 3 - You need to use "Set" with any object assignment, so use "Set pic2.Picture = Clipboard.GetData()".
|
Thu, 10 May 2012 22:43:44 GMT |
|
 |
Martin Trum #8 / 25
|
 Snapshot of screen
Quote: > I could think of three issues with the code you posted: > 1 - It would erase the clipboard, and this may annoy the user.
Accepted but you could save and restore it if necessary. Quote: > 2 - The code you posted would capture the active window only, while the > BitBlt method would capture any window unless it's obscured by other > windows, in which case it captures the area in the desktop that it would > usually occupy.
Yes, but I did say it could be adapted. Just omit the ALT stuff. Quote: > 3 - You need to use "Set" with any object assignment, so use "Set > pic2.Picture = Clipboard.GetData()".
Really? pic2.Picture = Clipboard.GetData() seems to work for me. Am I deluding myself? Often happens, I'm no expert :-)
|
Thu, 10 May 2012 23:25:15 GMT |
|
 |
Dave O #9 / 25
|
 Snapshot of screen
Quote: >> I could think of three issues with the code you posted: >> 1 - It would erase the clipboard, and this may annoy the user. > Accepted but you could save and restore it if necessary.
If you knew how difficult that might be you would not say that. The clipboard can contain the same information in several formats all of which you would need to store and replace, for example a selection of a Excel spreadsheet is stored in the clipboard as ALL of the following clipboard formats: Biff, Biff3, BIFF4, Biff5, Biff8, Bitmap, Csv, Data Interchange Format, DataObject, Device Independent Bitmap, Embed Source, Enhanced Metafile, HTML Format, Hyperlink, Link, Link Source, Link Source Descriptor, Locale Identifier, Metafile, Microsoft Symbolic Link, Native, Object Descriptor, ObjectLink, OEM Text, Ole Private Data, OwnerLink, Rich Text Format, Text, Unicode Text, Unknown: -16639, Unknown: -16640, Unknown: 129, Unknown: 17, Wk1 & XML Spreadsheet, That is an extreme example but just a bit of plain text is in these formats: Text, OEM Text, Unicode Text & Locale Identifier Also if you've ever placed a list of files onto the clipboard using code you'll know what fun that can be. Regards Dave O.
|
Fri, 11 May 2012 18:05:49 GMT |
|
 |
Dave O #10 / 25
|
 Snapshot of screen
Quote:
> I seem to recall a similar discussion... > http://groups.google.com/group/microsoft.public.vb.general.discussion... > "A while back" ? How about 8 years! <g> > LFS
That was a while wasn't it! Well the technique seems to work well enough, thanks again for your help back then when dinosaurs roamed the Earth. Dave O.
|
Fri, 11 May 2012 18:08:28 GMT |
|
 |
H-Ma #11 / 25
|
 Snapshot of screen
Quote:
>> Hi VB lovers, >> I'm using the following code to get a snapshot of the screen in a >> StdPicture object. >> http://www.devx.com/vb2themax/Tip/19172 >> However my problem is I need to get the space used (no of bytes) by the >> image in memory. Is this possible? If not I would like to convert the >> image into a JPG file and then get the size of file in bytes. >> Size itself is not required actually. I just want to compare two images >> and see if they are the same. The only way I can think of is to compare >> there size/space in memory or disk. If there is another efficient way to >> do this please advise. > No, you can not relay on size to determine if they are the same. > Nor can you rely on different (non bitmap) binary data to say they are > different. > Bitmaps in memory are a fixed (height x width x bytes per pixel) + > header (with a few caveats for odd sizes and bit depths) > Once compressed, the final image data may contain other data so you are > unlikely to get exactly the same data for multiple encodes of the same > image. > The only way to reliably tell is to do a pixel by pixel comparison. > If the images will have been subject to lossy compression, you will have > to have a tolerance in there as well. > What are you actually trying to achieve by seeing if the screen has changed?
right, size alone won't tell you anything for sure. That's what a ahsh function is for. Using a simple CRC32 function on the two data sets would tell you pretty much right away if the data is the same. -- HK
|
Sat, 12 May 2012 00:22:27 GMT |
|
 |
Dave O #12 / 25
|
 Snapshot of screen
Quote:
>>> Hi VB lovers, >>> I'm using the following code to get a snapshot of the screen in a >>> StdPicture object. >>> http://www.devx.com/vb2themax/Tip/19172 >>> However my problem is I need to get the space used (no of bytes) by the >>> image in memory. Is this possible? If not I would like to convert the >>> image into a JPG file and then get the size of file in bytes. >>> Size itself is not required actually. I just want to compare two images >>> and see if they are the same. The only way I can think of is to compare >>> there size/space in memory or disk. If there is another efficient way to >>> do this please advise. >> No, you can not relay on size to determine if they are the same. >> Nor can you rely on different (non bitmap) binary data to say they are >> different. >> Bitmaps in memory are a fixed (height x width x bytes per pixel) + >> header (with a few caveats for odd sizes and bit depths) >> Once compressed, the final image data may contain other data so you are >> unlikely to get exactly the same data for multiple encodes of the same >> image. >> The only way to reliably tell is to do a pixel by pixel comparison. >> If the images will have been subject to lossy compression, you will have >> to have a tolerance in there as well. >> What are you actually trying to achieve by seeing if the screen has >> changed? > right, size alone won't tell you anything for sure. That's what a ahsh > function is for. Using a simple CRC32 function on the two data sets would > tell you pretty much right away if the data is the same. > -- > HK
Here's a thought, JPEGs being lossy tend to corrupt the image enough to make comparisons tricky. BMPs are not lossy but compared to JPEGs have huge file sizes. Putting BMPs into a ZIP file can give as good, if not better compression than JPEG and depending on which library you use (I use InfoZip) you can get it to return the CRC of the file thus saving all that mucking about with CRC routines. So if he saves as BMP then sticks them into a ZIP file or files he can compare easily and instead of a JPEG library he'll need a ZIP library, so not really any change in overhead but a far simpler bit of coding. Regards - Dave O.
|
Sat, 12 May 2012 00:52:49 GMT |
|
 |
Peter #13 / 25
|
 Snapshot of screen
Quote:
>> 3 - You need to use "Set" with any object assignment, so use "Set >> pic2.Picture = Clipboard.GetData()". > Really? pic2.Picture = Clipboard.GetData() seems to work for me.
Yes both w/out Set work for me, in VBA too. I've never quite understood why. Regards, Peter T
|
Sat, 12 May 2012 17:39:24 GMT |
|
 |
Nobod #14 / 25
|
 Snapshot of screen
Quote:
>>> 3 - You need to use "Set" with any object assignment, so use "Set >>> pic2.Picture = Clipboard.GetData()". >> Really? pic2.Picture = Clipboard.GetData() seems to work for me. > Yes both w/out Set work for me, in VBA too. I've never quite understood > why.
Looking at Object Browser(F2), the default property for both is "Handle". So that line is equivalent to: pic2.Picture.Handle = Clipboard.GetData().Handle
|
Sat, 12 May 2012 18:35:27 GMT |
|
 |
Peter #15 / 25
|
 Snapshot of screen
Quote:
>>>> 3 - You need to use "Set" with any object assignment, so use "Set >>>> pic2.Picture = Clipboard.GetData()". >>> Really? pic2.Picture = Clipboard.GetData() seems to work for me. >> Yes both w/out Set work for me, in VBA too. I've never quite understood >> why. > Looking at Object Browser(F2), the default property for both is "Handle". > So that line is equivalent to: > pic2.Picture.Handle = Clipboard.GetData().Handle
AhHa, similar in VBA MSForms.Image.Picture - Property Picture As StdPicture StdPicture.Handle As OLE_HANDLE Default member of stdole.StdPicture Intuitively I guess .Handle = .Handle is a tad more efficient. Or is it, any reason to use or not use Set? Regards, Peter T
|
Sat, 12 May 2012 18:59:48 GMT |
|
|
Page 1 of 2
|
[ 25 post ] |
|
Go to page:
[1]
[2] |
|