How to check Image.Source doesn't contains URI or Stream? - c#

I have List of 3 Images of type Xamarin.Forms.Image. First image has URI as source, Second image has stream that as I have uploaded second image from Gallery and I don't have anything in 3rd image.
List<Image> Photos = new List<Image>();
Image URIImage=ImageSource.FromURI("https://xxx.amazonaws.com/profiles/ddddd-1658-4cf1-b62c-cccccccc_1.jpg");
Photos.Add(URIImage);
Photos.Add(await GetImageFromGallery()); //"/Users/text/Library/Developer/CoreSimulator/Devices/3C26B8DD-1E37-47A0-B164-810AE3EA4A4D/data/Containers/Data/Application/05BE5988-4F9F-4E17-BCC2-xxxxxxxx/Documents/IMG_0001.JPG"
Photos.Add(new Image() { Source = string.Empty });
Now I want to go through loop and want to get image from Photos that doesn't have source. Here it's 3rd image of Photos list.
Can anybody please help me?

After read the comment, I think the simple solution is checking the string converted from image.source.
You can use the two as condition
image.Source.ToString() is "File: "
or
image.Source.ToString().Contains("File: ")

Related

Xamarin Forms XAML - Unable to create multiple images from a list of URLs

I have a List where the string is the url of an image.
I am trying to dynamically create a grid of images with the source being each url.
I have tried binding but it doesn't seem to work. I also cannot access the x:Name of an image I created in Xaml. I am open to doing this in Xaml or in the code behind.
Any ideas of how to do this.
//imageList is a List<string> with your image urls in it
foreach(var i in imageList) {
Image image = new Image();
image.Source = ImageSource.FromUri(new Uri(i));
// you will need to add logic to calculate the Row/Column for each image
myGrid.Children.Add(image,x,y);
}

C#/XAML - Assets not shown when modified by code

I created a user control, which is made with images.
I tried to modify them by C# method, for example:
imgElement.Source = new BitmapImage(new Uri("ms:appx/Resources/IconElement/my_icon_element.png"));
imgBackgroundElement.ImageSource = new BitmapImage(new Uri("ms:appx/Resources/BackgroundElement/my_background_element.png"));
The soft finds images but doesn't show them ; the image control is empty.
If do not use the method and let images by default, they are shown...
Can anyone teach me why ? ^^
Thanks !
An absolute path for your image will be:
ms-appx:///Resources/IconElement/my_icon_element.png

Extracting a image from a WebBrowser Control

My aim is to extract an image from a loaded webbrowser control.
// get the image link from the image element ID.
string imageLink = wbVideos.Document.GetElementById("sbvdcapimg").GetAttribute("src").ToString();
//Download the image into a bitmap
Bitmap image = new Bitmap(new System.IO.MemoryStream(new System.Net.WebClient().DownloadData(imageLink)));
so the code works for most pictures, but i receive me a format error when i use it with the link below.
The error is thrown when i parse this link into my code: "http://www.swagbucks.com/?cmd=cp-get-captcha-image&ctype=1&tai=478817685&sid=0.4015013770493371"
(Please Note to view the image you need to login!)
Notice how the image does not end in a extension, this is most likely causing the error.
Example of the extracted link:
so, my question, how can i make my code accept this link as a valid image file?

Umbraco display images created from image cropper using razor

How can I display images created using the image cropper data type?
As an example say I upload an image1.jpg, this contains 3 image crops named thumb, featured and main.
How would I display the image created as thumb using Razor?
The following should point you in the right direction. The Image Cropper is a standard Umbraco Datatype so it has some support for accessing the crops, you can find them by crop name:
var mediaItem = Library.MediaById(Model.Thumbnail);
var img = mediaItem.crop.Find("#name", "thumbnail");
if (img != null)
{
<img src="#img.url" />
}
I hope that helps.
I have got this working, not sure it is best practice but does the job for now.
<img src="#Library.Concatenate(mFile.umbracoFile.Split('.')[0], "_thumb.jpg")" />
The above takes the filename of the media item, strips the file extension off and concatenates it to the cropped image name using the Library Helper. In this case the cropped name was thumb, so adding an underscore and the cropped name resolves the generated image.
The cropped images are generated within Umbraco on the server in the following format:
filename - fileextension + underscore + croppedname + fileextension

How to show Image from byte array in Microsoft report

I am using a Report file and a ReportViewer control to show a report which loads data dynamically from objects during run-time.
I need to show an image which is stored as a byte array in the object.
The PictureBox's value is currently set to:
=First(Fields!ImageData.Value, "dtstItemImage")
And I set the DataSource using:
ImageBindingSource.DataSource = this.item.Image.ImageData;
The code compiles and runs but the image is not displayed in the report.
Is this because the PictureBox needs to be bound to an Image object (and not to a byte array)? Or are there perhaps some properties of the PictureBox which I need to set?
UPDATE 1
I've added a border to the PictureBox just to make sure that's its visible and it does show up in the report. It just doesn't contain the image.
UPDATE 2
I've fixed a mistake in my code. I've changed:
ImageBindingSource.DataSource = this.item.Image.ImageData;
to:
ImageBindingSource.DataSource = this.item.Image;
as the PictureBox is bound to the ImageData field BUT the DataSource is the Image object.
Now I get a small cross icon instead of nothing which (at least for me) indicates some progress but I don't know where the byte[]-bitmap conversion code needs to be.
I managed to solve this by setting the report's Image box Source property to Database (it was previously set to External).
More info about the different available Source values can be found at (MSDN) HowTo: Add an Image (Reporting Services).
You need to create an image object from the byte array and use that as the source.
To do this, you can use a helper function like the following
public static Image LoadImage(byte[] imageBytes)
{
Image image = null;
using (var ms = new MemoryStream(imageBytes))
image = Image.FromStream(ms);
return image;
}
Edit
For WPF, you need to use BitmapSource (MSDN) instead of Image (MSDN)
public static BitmapSource LoadImage(Byte[] imageBytes)
{
var image = new BitmapImage();
using (var ms = new MemoryStream(binaryData))
{
image.BeginInit();
image.StreamSource = ms;
image.CacheOption = BitmapCacheOption.OnLoad;
image.EndInit();
}
if (image.CanFreeze)
image.Freeze();
return image;
}
NB: You can could also do this using a IValueConverter, see this blog post for the source code.
and then modify your data binding
ImageBindingSource.DataSource = LoadImage(item.Image.ImageData);
...
Make sure that the image (and MemoryStream) is disposed properly when you finished with it, as otherwise you will leak memory.
Also, depending on the format of your byte array you may need to do some work. See one of my question/answers for some helpful information.

Categories