Button image content wont show except after step by step debug - c#

I'm at a loss here...
Just trying to get an image showing on a button from codebehind. The image is being fed by the user in a dialog and is copied in the executable folder.
Image buttonImage = new Image();
buttonImage.Source = new BitmapImage(new Uri(Name + ".png", UriKind.Relative));
buttonImage.Height = 100;
Content = buttonImage;
It actually does work but only when stepping with the debugger. The code abode is from within my own Button control in a desperate attempt to try a slightly different way and see if the result would be different.
From what I've found, most people recommend setting Button Content Templates and use databinding from there, but it's not an option here.
Any idea why that wouldn't show up properly?
Thanks for any help you can give.
EDIT: this and the answer links have been studied to no avail
EDIT2: It's really when I'm changing a value within the Image Control at runtime that the image shows up afterward. I obviously tried to put the option changed directly into the code but it didn't work... (changed the stretch mode to be sure it wasn't a scale issue)

Related

WebView2 Source property doesn't initiate CoreWebView2

I've spent half an hour trying to figure out this tedious issue where you're unable to Navigate strings unless the Source property is set.
After all, I figured out a workaround hack since WebView2 demands an absolute path to an html file or nothing else.
Markdown.Focus();
Markdown.BringIntoView();
Markdown.Source = new Uri(Path.GetFullPath("null.html"), UriKind.Absolute);
Markdown.Visibility = Visibility.Visible;
Markdown.NavigateToString(htContent);
Even after all of this. It still says "You need to set the Source property!!". This is driving me nuts.
null.html is a valid html file too. It's just empty since the HTML I need to display is way too dynamic to buffer into a file.
Instead of the code, you have, try this:
await Markdown.EnsureCoreWebView2Async();
Markdown.NavigateToString(htContent);
Now you don't have to set the Source property.
BTW: You don't have to set the other properties either, the WebView2 Control is automatically displayed.

My WPF UserControl can't locate resources when I use it with a project other than the one it was built in

So I built a WPF user control as part of a larger solution. I'm building another app where this user control would work nice. I added a reference to the DLL from the new app, wired it up and compiled. The window loads and I can see the image that it's telling me it can't find. After the main window displays and the user control is populated, an exception is thrown saying...
System.IO.IOException: 'Cannot locate resource 'resources/nosortnofilter.png'.'
The user control is a DataGrid with some extensions added to it. The column it threw on was "id". As you can see in the image, the red arrow shows the nosortnofilter.png image being displayed in all columns. So why is it throwing this exception?
The line of code it throws on is here.
If ImageName = "" Then ImageName = "NoSortNoFilter"
img.Source = New BitmapImage(New Uri("pack://application:,,,/Resources/" & ImageName & ".png"))
So it all looks good from my perspective. Hoping someone can see what I'm not seeing.
EDIT: Found a solution. This works. But it still doesn't answer the questions why the original pack:// formatted URI only worked with the original solution.
img.Source = New BitmapImage(New Uri($"Resources/{ImageName}.png", UriKind.Relative))
EDIT: Thanks to rfmodulator for giving me the correct URI for DLL's.
img.Source = New BitmapImage(New Uri("pack://application:,,,/AdvancedSortFilterDataGrid;component/Resources/" & ImageName & ".png"))
This is the URI of a resource in the Application's assembly:
pack://application:,,,/Resources/[RESOURCENAME]
To get a resource in a DLL, the URI looks more like this:
pack://application:,,,/[DLLNAME];component/Resources/[RESOURCENAME]
For details, see Pack URIs in WPF.

How do I copy an image to the clipboard in Selenium?

I am writing a test and the functionality I need to replicate is essentially saving a image to the clipboard and paste it later on. I am using Selenium WebDriver v3.11.1.
I have attempted using ContextClick to copy an image in many various ways and it never quite did what I wanted for example:
Actions rightClickAction = new Actions(driver);
rightClickAction.MoveToElement(logo).ContextClick(logo).SendKeys(Keys.ArrowDown).SendKeys(Keys.ArrowDown).SendKeys(Keys.ArrowDown).SendKeys(Keys.Enter).Build().Perform();
But the arrow down/enter never worked because it didn't focus on the right click menu. So then I found this bug https://bugs.chromium.org/p/chromedriver/issues/detail?id=1003 which makes me think that I can't use context click to copy an image. I also, couldn't just 'ctrl+c' the image.
I then learned that I could Clipboard which I couldn't get to set an image from my directory:
Clipboard.SetImage(Image.FromFile("C://Image.png"));
I then tried taking a screenshot as done here: C# Selenium - How do you take a screenshot in Visual Studio 2015 and that didn't work with either. Trying to save the screenshot file and add it to the 'clipboard' got messy.
I have also tried grabbing an image from a page by getting a base64 string of the image with JavaScript that is executed by webdriver, then saving the base64 string of the image to a file, which I found here: Using selenium to save images from page
This also got messy and I wasn't sure how to then save it to the clipboard.
So, how can I save an image to my clipboard?
You can try something similar to this:
driver.get("https://stackoverflow.com/");
WebElement element = driver.findElement(By.xpath("//span[(text()='Stack Overflow') and #class='-img _glyph']"));
WrapsDriver wrapsDriver = (WrapsDriver) element;
File screenshot = ((TakesScreenshot) wrapsDriver.getWrappedDriver()).getScreenshotAs(OutputType.FILE);
Rectangle rectangle = new Rectangle(element.getSize().width, element.getSize().height, element.getSize().height, element.getSize().width);
Point location = element.getLocation();
BufferedImage bufferedImage = ImageIO.read(screenshot);
BufferedImage destImage = bufferedImage.getSubimage(location.x, location.y, rectangle.width, rectangle.height);
ImageIO.write(destImage, "png", screenshot);
File file = new File("C:\\tmp\\123.png");
FileUtils.copyFile(screenshot, file);
Let me know if it works for you

adding images to an HTML Canvas Generated by C#

Hello so I have an interesting question for everyone. I have a WPF application that generates an html preview of a report based on Database data and user input. My currently dilemma is that I have a HTML canvas that is created by C# as follows:
Canvas myCanvas = new Canvas();
myCanvas.Name = "cvsMyCanvas";
myCanvas.width = userDefinedByTtextBox;
myCanvas.height = userDefinedByTtextBox;
myCanvas.Visibility = Visibility.Collapsed //Don't show the canvas just yet
My question is how do I get an image that I have saved locally to the computer on the canvas in a specific position. (i.e. A HTML Image tag object that is generated by C# and use the move to functionality)
(This is a rough example of how I think it should work but this is what I need the help on)
Example:
string htmlImage = "<img id=\"myImage\" src=\"C:\\Temp\\img.png\">";
htmlImage.moveTo(x,y);
...again the above code is an example of what it is I am trying to do. If anyone could help me out that would be great! Also if I did not provide enough information please let me know and I will let provide it.
There seems to be some confusion on what it is I am trying to do, it is most likely my fault do to lack of explanation so allow me to elaborate more. I have images saved in a folder in my temp directory I. I am running a WPF application that has a WebBrowser control that I am using to display HTML. The HTML that is used in that WebBrowser is created in the C# code behind. I am trying to create a canvas that is displayed in the WebBrowser with the images that I have saved in my temp folder. The code I have provided above apparently will not work for what I am trying to accomplish because there is no way to create a canvas in C# and use it in HTML. Let me know if this helps shed some light on this predicament or if even more explanation is required.
If you want to generate HTML5 code from your C#, the HTML code to generate should look like that:
context = canvas.getContext('2d');
var image = new Image();
image.src = <your path>;
image.onload = function() {
context.drawImage(image, x, y, w, y);
}
Then, just store it in the String or StringBuilder

Image not loading correctly from BitmapImage in code behind

Okay, I'm attempting to load an image to an Image control on my page. Now as far as I know, I know how to do this (the method I'm using has worked before). But for some reason it's not working this time. The image is not being loaded.
Okay, let's start with the XAML. You can see it here.. Please note my excellent artistic skills. You can see that the top image is the left one on the screen and the bottom image is the right one. As you can also see, the image on the right is loaded in the XAML from a file in the Assets/Images folder. It should also be noted that we can take from this that the image loads okay - there aren't errors with the image. We can also see that the images are not blocked by anything (when the program runs, the right image shows just fine).
So the left image, 'image1' is the one that I'm loading from the code behind. Here is the code behind for that page. As you can see, there's not a lot. Yes I do use a view model, but it doesn't interface with the image at all (its only exposed properties are an Entity (for a selected item) and an ObservableCollection of entities (for the ItemsSource of a control)). As you can also see, I'm attempting to load the same image as is used for the static image (so I can say for sure that the image is okay for Silverlight).
If you're wondering about the BitmapCreateOptions line, that was something that someone suggested to me to add. I have tried removing it, but that doesn't seem to cause any difference.
The next question is - have I attempted to use the event handler for the failure? Yes I have. The error I get is 'AG_E_NETWORK_ERROR'.
I'm not sure what exactly this is about - I'm not loading from a network.
Can anyone offer assistance? Thanks.
I can't help but notice that in your XAML, you have a "../" at the start of the image path, but this is missing from the path you're using in code-behind. Could this be the problem?
Have You tried to call BeginInit and EndInit before and after setting the uri?
BitmapImage bmpImg = new BitmagImage();
bmpImg.BeginInit();
bmpImg.UriSource = new Uri(...);
bmpImg.EndInit();
Or use the constructor that takes an Uri. msdn

Categories

Resources