This is for developing a Windows 10 application.
I am trying to capture an image using the camera, edit the captured image,
CameraCaptureUI captureUI = new CameraCaptureUI();
captureUI.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Jpeg;
captureUI.PhotoSettings.AllowCropping = true;
captureUI.PhotoSettings.CroppedAspectRatio = new Windows.Foundation.Size(4, 3);
//captureUI.PhotoSettings.CroppedSizeInPixels = new Windows.Foundation.Size(0,0);
captureUI.PhotoSettings.MaxResolution = Windows.Media.Capture.CameraCaptureUIMaxPhotoResolution.HighestAvailable;
StorageFile file = await captureUI.CaptureFileAsync(CameraCaptureUIMode.Photo);
This shows up the camera, and I am able to preview and click to capture the image. The captured image is shown in another window over the original camera image windows. I am able to crop and click to save this captured image.
However, right after I click save, the image is getting stretched, and after it is saved, it remains stretched as well. In essence, the image I capture and save is being stretched,
I tried changing the CroppedSizeInPixels with size from (0,0) to (200,200). And also various CroppedAspectRatios.
What am I doing wrong? Should I be using any other values for these settings?
Any help here is appreciated.
Related
I have a WPF-Application which hosts a WinformsPanel
<WindowsFormsHost>
<windowsForms:Panel
x:Name="PlayerHost">
</windowsForms:Panel>
<WindowsFormsHost>
I then use this Panel to display a video. I'm using Mpv.NET lib to do this.
The video player is initialized properly:
//panel.Handle is the windowsForms:Panel named PlayerHost
player = new MpvPlayer(panel.Handle, Common.IO.FindLib.FindMpvLib(binaryPath));
player.Load(videoFilePath);
Now, if I try to draw the panels content, the resulting image remains blank. The code to draw the image is as follows:
using (var bmp = new Bitmap(panel.ClientSize.Width, panel.ClientSize.Height))
{
panel.DrawToBitmap(bmp, panel.ClientRectangle);
bmp.Save(#"Some:\path.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
}
To make it clear: The video gets displayed. I can see the video content. And I start drawing to an image when the media is properly loaded. I even offloaded the drawing command to a button click. So while the video was running I tried to take a "frame capture" so to say. But the image remains blank.
How can I capture the panels content? Has it something to do with the native Handle being provided to the video player? Thanks in advance.
You can save the panels content to a bitmap with the following code:
using (Bitmap b = new Bitmap(panel.Width, panel.Height))
{
panel.DrawToBitmap(b, new Rectangle(0, 0, b.Width, b.Height));
b.Save("test.png");
}
CameraCaptureUI captureUI = new CameraCaptureUI();
captureUI.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Jpeg;
captureUI.PhotoSettings.MaxResolution = Windows.Media.Capture.CameraCaptureUIMaxPhotoResolution.HighestAvailable;
StorageFile file = await captureUI.CaptureFileAsync(CameraCaptureUIMode.Photo);
This shows up the camera, and I am able to preview and click to capture the image. The captured image is shown in another window over the original camera image windows. I am able to crop and click to save this captured image.
However, right after I click save, the image is getting stretched, and after it is saved, it remains stretched as well. In essence, the image I capture and save is being stretched,
I tried removing the crop and it works fine, but I need the crop feature as well.
What am I doing wrong? Should I be using any other values for these settings?
This was working fine till December if I remember correctly.
Any help here is appreciated. Screenshots added below
, ,
I am new to C#, I am using Visual Studo 2010, I have an image that needs to be displayed on a picturebox.
I tried many different methods. All the methods results in some unwanted pixels to appear along with image.
I have tried
picturebox.Image = Image.FromFile("bird.png");
result->
the image is displayed but with the stray white/black pixels at random places.
I also tried creating a bitmap of same size and drawing the image onto the bitmap and then assigning the bitmap to the picture box.Image. Still those unwanted pixels are visible.
I have tried clearing the picture box image (filling it with white or transparent) and then assigning the image, still same error occurs.
PS: this doesn't happen for all the images only certain images show this behaviour.
any help would be great
Code:
Image org = Bitmap.FromFile("bird.png");
Bitmap final = new Bitmap(org.Width,org.Height);
using (Graphics g = Graphics.FromImage(final))
{
g.DrawImage(org,0,0,GraphicsUnit.Pixel);
}
picturebox.Image = final;
if i use final.save("picture.png"). The "picuture.png" does not have that wrong pixels, it happens only when i use picture box to display it.
Please find the images attached defect orginal
PS: its not because of different fileformat (orginal and defect)
It was an TransperancyKey issue resolved by setting it to Default.
I've seen a ton of examples how to save an image based on saving a screenshot.
This has a fundamental flaw. The take screenshot only takes whats visable on the page at the time. So if I have an image at the bottom of the page, and I want to save it based on the location of the elements that I found, either one or two problems occur.
If I save the screenshot and then try to save by the location, the screen shot ends at point 1200 but the image is located at 3000. If i focus on the image and then take a screen shot, the image is is now on the screen shot, however, the location doesn't work. It doesn't work because I still have a 1200px height image with a location of 3000.
How can I simply say, I have an image at 3000x 3014y and I just want to save it?
You can basically get base64 string of the image and save it to file.
var base64string = driver.ExecuteScript(#"
var c = document.createElement('canvas');
var ctx = c.getContext('2d');
var img = document.getElementsByTagName('img')[0];
c.height=img.height;
c.width=img.width;
ctx.drawImage(img, 0, 0,img.width, img.height);
var base64String = c.toDataURL();
return base64String;
") as string;
var base64 = base64string.Split(',').Last();
using (var stream = new MemoryStream(Convert.FromBase64String(base64)))
{
using (var bitmap = new Bitmap(stream))
{
var filepath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"{Guid.NewGuid()}.jpg");
bitmap.Save(filepath, ImageFormat.Jpeg);
}
}
I've resolved it. Chrome vs IE vs Firefox take screen shots in different manners. Firefox will stitch the screenshot together, chrome only takes whats visible and IE will shrink everything to try to fit on one page.
If you need to do a screenshot of something that is not on the base page i'd suggest firefox
I'm currently using Selenium WebDriver 2.35 and hit a roadblock when it comes to taking a screenshot. I wrote up a small function that takes in an IWebElement and will return a screenshot of the specific element. The element I am trying to screenshot is actually an image pulled from a sprite. This element is tricky though, because upon mouseover/hover the image changes from gray to its true color(by moving to a different part of sprite). I am able to get a proper screenshot of the image via this function, but cannot get it to recognize the mouse interactions with ITakesScreenshot. I can visually see in the browser that the image is hovered over, but the screenshot cannot. Any thoughts?
public static Bitmap GetImage(IWebElement element)
{
RemoteWebDriver driver = BrowserManager.GetInstance().GetDriver();
Actions action = new Actions(driver);
//take screenshot of page
action.MoveToElement(element).Build().Perform();
Byte[] ba= ((ITakesScreenshot)driver).GetScreenshot().AsByteArray;
Bitmap ss = new Bitmap(new MemoryStream(ba));
//ss.Save("c:\\tmp\\ss.png", ImageFormat.Png);
Rectangle crop = new Rectangle(element.Location.X, element.Location.Y, element.Size.Width, element.Size.Height);
//create a new image by cropping the original screenshot
Bitmap image = ss.Clone(crop, ss.PixelFormat);
return image;
}
In my experience, with automations that route through Selenium Grid, no mouse is seen. Perhaps this is because the "mouse" is actually a virtual Selenium mouse and unconnected to the systems native mouse driver.
It seems as though the newest selenium (2.39) has addressed this issue and I am able to see mouse hover in this screenshot method. Thanks everyone for your help!