Why this don't work javaFX Oracle example - image

This is example on site Oracle and they say this working.
// load the image
Image image = new Image("file_name.png");
// simple displays ImageView the image as is
ImageView iv1 = new ImageView();
iv1.setImage(image);
On site is picture result code. But it did not work for me. I see exception
java.lang.IllegalArgumentException: Invalid URL or resource not found.

Probably outdated. Try this.
File selectedFile = new File("yourfile.png");
String filePath = selectedFile.getAbsolutePath();
Image image = new Image(selectedFile.toURI().toURL().toString());
ImageView iv1 = new ImageView();
iv1.setImage(image);

Related

Unity changing from game object to ui Image

In unity I am fetching a UI image through GameObject.Find() However when I try to change the value of another image component to the result of the game object find I get the error:
Cannot implicitly convert type 'UnityEngine.GameObject' to 'UnityEngine.UI.Image' \[Assembly-CSharp\]csharp(CS0029)
Code:
Image image;
image = GameObject.Find("the other image");
Is there a way to fetch a UI Image game object through just it's name without it being set to a generic game object?
You are attempting to assign the value of a image to a gameObject, you simply need to get the image
Image image;
var imageGameObject = GameObject.Find("the other image");
if(imageGameObject == null)
{
//couldn't find the gameobject
}
image = imageGameObject.GetComponenet<Image>();
to get the actual image component

How to convert WritableImage to ImageView in javaFX?

Hello Stackoverflow members, have a great day. Is there a way to convert or cast a writable image object to ImageView in order to display it in my program without the need to save the image on a hard drive.
WritableImage writable_image = barchart.snapshot(new SnapshotParameters(), null);
I need something that converts writable_image to ImaveView object.
You don't convert a WritableImage to an ImageView. An ImageView is a UI component designed to show an Image. Since WritableImage extends Image you don't have to do anything special.
ImageView imageView = ...;
WritableImage writableImage = barchart.snapshot(new SnapshotParameters(), null);
imageView.setImage(writableImage);

JavaFX show photos in ImageView not working

I'm trying to display a photo depending on the BMI of the user.. So for example if your BMI is under 18.5 it will show a thumbs down.
All the images are located in my resource folder. But the image doesn't show up in the program as it should.. I've tried serveral images to see if it had something to do with the resolution. This is my code:
#FXML
private void tellUserStatus(){
date = new Date();
if (bmi<18.5){
System.out.println(dateFormat.format(date)+"User is underweight - BMI is under 18.5");
BmiStatusMessage.setVisible(true);
BmiStatusMessage.setText("You are underweight");
File underweight= new File("underweight-thumbsdown.png");
Image image = new Image(underweight.toURI().toString());
BmiConclusion.setVisible(true);
BmiConclusion.setImage(image);
}
}
BmiStatusMessage is a Label, and BmiConclusion is an ImageView. They are both linked with the fxml file.

How to convert BitmapImage to WriteableBitmap in Universal application for windows 10?

I am implementing a Universal Windows Application on Windows 10 using MVVM.
I have a file picker allowing me to choose an image. This image is displayed in an Image Control. The source of the Image Control is bound to a property in my view model. This property is a byte array. I need a converter to convert the BitmapImage in a Byte Array.
I have read lots of things but can't found something working.
I have found interesting stuff on https://writeablebitmapex.codeplex.com/
but if i want to use this package i need a WriteableBitmap and not a BitmapImage.
Thank you in advance for your help.
You can load the image to a WriteableBitmap object straight from the file.
var filePicker = new FileOpenPicker();
filePicker.FileTypeFilter.Add(".jpg");
var result = await filePicker.PickSingleFileAsync();
if (result != null)
{
using (IRandomAccessStream stream = await result.OpenAsync(FileAccessMode.Read))
{
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);
WriteableBitmap bmp = new WriteableBitmap((int)decoder.PixelWidth, (int)decoder.PixelHeight);
bmp.SetSource(stream);
// show the image in the UI if you want.
MyImage.Source = bmp;
}
}
This way you have the WriteableBitmap and you can use the WriteableBitmapEx library.

Saving an image from image control to gallery in windows phone 7

I have a image in a Image control like below:
<Image x:name="myImg" Source="Images/MyImg.png" />
How can I save this image in to the image gallery so that I can see by going to the gallery folder.
I have tried different codes but I am not able to save it. Please help me on this.
EDIT:
I have image in control in List Box. I am binding the list box with the IList that is coming from the web service.
So after Binding the image if user want to save the image he can save the particular which he want to save.
So how can i save the that particular image.
Thanks in advance.
There is an excellent MSDN article that describes this very scenario:
How to: Encode a JPEG for Windows Phone and Save to the Pictures Library
Incidentally , it's also the first search result link when searching for windows phone save image to media library on both Google and Bing.
Have you tried following this guide, and if so what are you having problems with?
The essential code to save it:
private void btnSave_Click(object sender, RoutedEventArgs e)
{
// Create a file name for the JPEG file in isolated storage.
String tempJPEG = "TempJPEG";
// Create a virtual store and file stream. Check for duplicate tempJPEG files.
var myStore = IsolatedStorageFile.GetUserStoreForApplication();
if (myStore.FileExists(tempJPEG))
{
myStore.DeleteFile(tempJPEG);
}
IsolatedStorageFileStream myFileStream = myStore.CreateFile(tempJPEG);
// Create a stream out of the sample JPEG file.
// For [Application Name] in the URI, use the project name that you entered
// in the previous steps. Also, TestImage.jpg is an example;
// you must enter your JPEG file name if it is different.
StreamResourceInfo sri = null;
Uri uri = new Uri("[Application Name];component/TestImage.jpg", UriKind.Relative);
sri = Application.GetResourceStream(uri);
// Create a new WriteableBitmap object and set it to the JPEG stream.
BitmapImage bitmap = new BitmapImage();
bitmap.CreateOptions = BitmapCreateOptions.None;
bitmap.SetSource(sri.Stream);
WriteableBitmap wb = new WriteableBitmap(bitmap);
// Encode the WriteableBitmap object to a JPEG stream.
wb.SaveJpeg(myFileStream, wb.PixelWidth, wb.PixelHeight, 0, 85);
myFileStream.Close();
// Create a new stream from isolated storage, and save the JPEG file to the media library on Windows Phone.
myFileStream = myStore.OpenFile(tempJPEG, FileMode.Open, FileAccess.Read);
// Save the image to the camera roll or saved pictures album.
MediaLibrary library = new MediaLibrary();
if (radioButtonCameraRoll.IsChecked == true)
{
// Save the image to the camera roll album.
Picture pic = library.SavePictureToCameraRoll("SavedPicture.jpg", myFileStream);
MessageBox.Show("Image saved to camera roll album");
}
else
{
// Save the image to the saved pictures album.
Picture pic = library.SavePicture("SavedPicture.jpg", myFileStream);
MessageBox.Show("Image saved to saved pictures album");
}
myFileStream.Close();
}

Resources