I am using the managed Graphics.CopyFromScreen method to take a Bitmap screenshot of a region on the screen.
Everything is working using the CopyPixelOperation.SourceCopy enum as the flag to the CopyFromScreen method... but unfortunately I need to capture layered / transparent windows that are in the region I'm trying to capture... and with only the SourceCopy enum these do not get picked up in the resulting image. This can be solved using the CopyPixelOperation.CaptureBlt... but I can't find a way to do this:
Graphics.CopyFromScreen(left, top, 0, 0, size, CopyPixelOperation.SourceCopy | CopyPixelOperation.CaptureBlt); // note the binary OR operator
... as this results in a "Bitwise Or is not possible on type enum" style error from Resharper - upon which I researched the error discovering that bitwise operations on enums happens on the numeric representation of the enum values and you will end up with another one of the available enum values (opposed to two of them).
Is there a way to pass two enums to this function or a similar function? I have a preference in staying managed if possible.
MSDN Documentation
CopyFromScreen
CopyPixelOperation
As per MSDN:
copyPixelOperation
Type: System.Drawing..::.CopyPixelOperation
One of the CopyPixelOperation values.
You can't combine those flags, you probably want to use CopyPixelOperation.CaptureBlt alone anyway.
Related
My question is pretty similar to this one and I'm afraid the answer is the same... I want to save all the shapes/images on a slide as a single png (or jpeg). Programmatically, I get as far as
slide.Shapes.SelectAll();
but don't see a way to save as image. Is this possible? If not, any other suggestions, hopfully w/ examples? (not VBA - I need to automate the whole conversion)
There was a reference to OpenXML in the other post, but I'm not even sure how to pull that in.
I don't know how you'd do this in C# but I'd guess that you'd make use of the same methods as you would with VBA, where you can do:
Activewindow.Selection.ShapeRange.Export( "c:\temp\delete-me.jpg",ppShapeFormatJPG)
ppShapeFormatJPG is a PowerPoint constant, a VBA Long = 1; IIRC that'd be an Integer in C#.
The method also can take two more optional parameters, scalewidth and scaleheight, which govern the width and height of the exported image in undocumented ways. By default, no parms supplied, I get exports at 72 dpi. Larger numbers result in higher pixel count exports but distorted proportions. I'm sure there's some strange logic to it, but it escapes me; all hints welcome!
There's a third optional parm, ExportMode. In my tests, it makes no difference whether you supply it or not, and if you do, which of the available values you choose.
I have a loop that gets pixelcolors from an image and try to see if they are the same as the Color I passed into the method as parameter.
I tried the Equals method but it doesn't work. I also tried the ToKnown method.
It looks like that match doesn't work beacuse the values that synthesize the two colors don't match.
Example:
With GetPixel:
{Name=ff000000, ARGB=(255, 0, 0, 0)}
Color.Black:
{Name=Black, ARGB=(255, 0, 0, 0)}
if (pixelColor.ToArgb().Equals(startingOffsetColor.ToArgb())) { }
The code above works, but I still want to know if there is any better method or any method that can reduce any CPU overhead, because I'm using this inside a loop statement.
According to MSDN, the Color.Equality Operator...
...compares more than the ARGB values of the Color structures. It also does a comparison of some state flags. If you want to compare just the ARGB values of two Color structures, compare them using the ToArgb method
So the method that you are using is correct for comparing the raw values
EDIT
.ToArgb() returns an int so you can just use == for comparison, you don't need to use .Equals() if you find it too verbose.
Is it possible to compare two images with a mask for area's that do not need to be compared.
I managed to get it working with a basic file comparison
[UseReporter(typeof(BeyondCompareReporter))]
public void ThenThePageShouldMatchTheApprovedVersion()
{
SaveScreenshot("page1");
Approvals.VerifyFile(#"C:\page1.png");
}
But i would like to create a mask of the area's i expect to change. Is this possible with ApprovalTests or will i need to modify the screenshot and manually apply the mask before comparing with the approved file. Or is it possible to write your own validators?
It's not possible to mask the area so the comparer will not compare them.
However, it is very easy to actually mask the area (ie, place a black square over the area before you call Verify)
Alternatively, you can usually mock out the variable that is changing.
Details on Comparer:
ApprovalsFileComparer is a very stupid comparer. It knows nothing about file formats and has no idea of what an image is. It simply compares byte to byte. This simplicity allows it to work everywhere, but removes the ability to be smart about stuff. This is usually not an issue as the reporters are very very smart. Able to render and compare and do subtractive diffs and the like.
Happy Testing!
In a C# project, I ve got error using the AddImageFilter which is provided in the SimpleITK. Is there a common mistake that happens when trying to add two images with this filter? For example, maybe there is a rule the images should be both double or int.
The error I get is:
Image2 for AddImageFilter doesnt match type or dimension!
In a certain sense, your supposition is right. I couldn't been able to find the exact error you've got, but I found this sitkAddImageFilter implementation on GitHub. If you look at the AddImageFilter::Execute() function, at line 33, you'll find this exception been throwed:
std::cerr << "Both image for add filter don't match type or dimension!" << std::endl;
that seems related to yours (maybe the slight difference is just related to a different version of ITK). And that exception is throwed whenever this is verified:
if ( type != image2->GetDataType() || dimension != image2->GetDimension() )
So, a condition for the AddImageFilter is that both dimensions must be the same, and the metadata associated with the images must agree. This makes sense, because matrices addition is doable only when their dimensions match (and, of course, when they contain the same kind of informations).
If you are trying to add two different kinds of images (as an example: a DICOM with a TIFF), I suggest to convert at least one of them, in a way to have both of them in the same "metadata space".
The error message is a little vague and should be improved.
Many filters which take more than one image as input expect the following that pixel type, dimension, size, spacing and orientation to be same. The error message you got indicates that the pixel type or the size do not match. Likely emanating from the code generated from is line:
https://github.com/SimpleITK/SimpleITK/blob/master/TemplateComponents/ExecuteNoParameters.cxx.in#L8
I'd recommend printing your two images as strings to examine the meta-data to determine the difference.
I'm a little confused and not sure how or if this is possible, but is it possible to create custom ones? VertexElementFormat doesn't contain the type I want to use which is byte3 and I have no idea how I could add that to it :/.
No, you can't. For your purposes, you can probably use Byte4 or Color.
These settings tell the GPU how to decode the data you send it and place it in shader registers. They are not extensible.
Note that you can have more than one Position or more than one Color (for examples) by setting the UsageIndex in your VertexElement.