I have a directory with .png images which I display in a third party combobox of my c# program. So the user is able to choose one of this images using the combo box. Basic code used:
Bitmap thump = new Bitmap(<path>);
ComboItem item = new ComboItem();
item.Image = thump;
MyComboBox.Items.Add(item);
Now I would like to update one of this images at runtime. Unfortunately I can't delete the old image because it is still opened in my program, so somehow I either need to close it or open it in a way that does not keep the image in use by my program. The changes to the bitmap are not done in my program, I just pass the path of the dirctory to another program which saves the bitmap there (but fails at the moment because it can't delete the old bitmap).
I guess this is a simple problem but I could not find a solution here or on the internet.
First read the file to memory, then create the Bitmap using that data.
var m = new MemoryStream(File.ReadAllBytes(filename));
Bitmap thump = (Bitmap)Bitmap.FromStream(m);
I need to change the background image at runtime, based on which RadioButton the user clicks. I'm doing this in a WPF project in Visual Studio, and I need to put the code in the Checked event in the xaml.cs file
I have an Image control called imgBackground, with 6 images in its Source collection, which are listed in an Images folder in the Solution Explorer.
I've tried:
this.imgBackground.Source = "filename.jpg";
both with and without the quotes, and with various paths (I've tried too many different variations to list them all here) and nothing works - everything I've tried just gives an error in the editor, before I even try to build and run anything (the error given varies depending on what I'm trying at the time).
If you are using relative paths as filenames like
this.imgBackground.Source = "filename.jpg";
then these files must be in the same directory as the .exe of your program is.
One workaround would be to use absolute paths like
this.imgBackground.Source = #"C:\MyFolder\MyProject\filename.jpg";
Or, even further use the packaging mechanism of WPF or pack your images as resources into your assembly. Look at this thread.
EDIT:
For your clarification:
The Source-property demands an System.Windows.Media.ImageSource-object, which you must provide.
Do it like this:
BitmapImage bi3 = new BitmapImage();
bi3.BeginInit();
bi3.UriSource = new Uri("filename.jpg", UriKind.Relative);
bi3.EndInit();
this.imgBackground.Source = bi3;
Please refer to this documentation here.
I'd like to pre-visualize an image in a image box before save it in a directory.
How can i do this, i use a checkbox to see if the user wants to pre-visualize or not because i dont find another way to do this without a checkbox.
I use file upload to upload the image.
string serverFileName = "";
serverFileName = Path.GetFileName(Upload.PostedFile.FileName);
Upload.PostedFile.SaveAs(MapPath("~/fotosPerfil/") + serverFileName);
i use this piece of code to save the image.
I think this post is exactly what you need to implement. Check out Ivan's solution.
Yes, indeed you can read the path selected by the user and display the image in an <img> tag, all client-side prior to uploading.
I am looking for ways to save the output of a Chart control as an image on the hard drive. Is it possible in SL? as I am not sure so thought of putting a question here..
Thanks...
have a look here: Can I programmatically capture snapshot of a Silverlight User Control?
You can simply take a screenshot of your chart. If you want to put in on the HD with silverlight you need to open a SaveFileDialog. Then it is possible.
EDIT: If you want to save it in different formats use ImageTools. http://imagetools.codeplex.com/. If you use ImageTools you can get a stream like this:
var editBitmap = new WriteableBitmap(1024, 786);
editBitmap = new WriteableBitmap((int)(this.RenderSize.Width), (int)(this.RenderSize.Height));
editBitmap.Render(this, new MatrixTransform());
editBitmap.Invalidate();
var myImage = editBitmap.ToImage();
Stream stream = myImage.ToStreamByExtension("png");
Hope this helps.
BR,
TJ
I've got your average e-Commerce app, I store ITEM_IMAGE_NAME in the database, and sometimes managers MISSPELL the image name.
To avoid "missing images" (red X in IE), every time I display the list of products, I check the server for the image related to the product, and if that file doesn't exist - I replace it with default image.
As far as i can tell this doesn't affect performance, but I was wondering if there are any alternatives to fix a "missing image" problem.
I'm using ASP.NET + C# (.NET 3.5)
Some code:
foreach (Item item in Items)
{
string path = Path.Combine("~/images/", item.categoryImage);
item.categoryImage = File.Exists(Server.MapPath(path)) ? item.categoryImage : GetImageDependingOnItemType();
}
You might consider something with javascript
<img src="image.jpg" onerror="this.onerror=null;this.src='default.jpg'">
Edit: Or program a 404.aspx returning a default image, if a nonexisting image was requested.
<style>
.fallback { background-image: url("fallback.png"); }
</style>
<img class="fallback" src="missing.png" width="400" height="300">
If missing.png loads, it will cover the space allocated for it, as if the fallback were not specified. (Assuming it's not transparent.)
If missing.png fails to load, the space will instead be filled with fallback.png. You'll still get a little "broken image" icon, but I prefer it that way... a little hint that says "fix me".
If your images aren't all the same size, you'll notice that the background tiles by default. You can use background-repeat: no-repeat; if you don't like that.
I like Erik's solution, but without removing the event after the first execution, because if you are using that code in, let's say, an MVC partial view, this will work only the first time it is loaded. So I'd go with:
<img src="image.jpg" onerror="this.src='default.jpg';" />
In case you have many images in the same situation, like a grid, you can do this instead:
$("img").on("error", function () {
$(this).attr('src', 'default.jpg');
});
Of course, you may want to use a more specific jQuery selector.
You can specify on the server what image should be returned for all requests to non-existent image files. That way the user can get a "2 AWESUM 2 SHO" lolcat instead of a red x.
I think your way is pretty much OK. I'd do it in a function or do in .categoryImage accessor.
I think I would find a way to make the data consistent rather than allowing users to enter inconsistent data. Perhaps your management app could allow the manager to select an existing image or upload a new one, then set the name of the image based on this input so that you'd be assured that the image will exist. Only remove an image when all references to it have been removed from the database. Restrict the interaction with the data to your app so that people can't make those sorts of mistakes.
Another way to handle this would be to have a handler (or a controller in ASP.NET MVC) that does the image lookup based on id and returns the image data. Coupled with caching this could be very efficient and would allow you to do image replacement as well.