How to get/change Image Source name with code in Unity? - unityscript

we made a script and need to get the image name, we implement this task with a clumsy way below, but we still want to know whether there is a way to get the image name directly?
public class CubeManager
{
static public Dictionary<CubeType, string> cubeTypeDict = new Dictionary<CubeType, string>();
static private Dictionary<string, GameObject> cubeDict = new Dictionary<string, GameObject>();
static CubeManager()
{
cubeTypeDict.Add(CubeType.Wall01, "Wall01");
cubeTypeDict.Add(CubeType.Wall02, "Wall02");
cubeTypeDict.Add(CubeType.Wall03, "Wall03");
cubeTypeDict.Add(CubeType.Wall04, "Wall04");
cubeTypeDict.Add(CubeType.Wall05, "Wall05");
cubeTypeDict.Add(CubeType.Wall06, "Wall06");
cubeTypeDict.Add(CubeType.Wall07, "Wall07");
cubeTypeDict.Add(CubeType.Wall08, "Wall08");
cubeTypeDict.Add(CubeType.Wall09, "Wall09");
cubeTypeDict.Add(CubeType.Wall10, "Wall10");
cubeTypeDict.Add(CubeType.Wall11, "Wall11");
cubeTypeDict.Add(CubeType.Wall12, "Wall12");
cubeTypeDict.Add(CubeType.Wall13, "Wall13");
cubeTypeDict.Add(CubeType.Wall14, "Wall14");
cubeTypeDict.Add(CubeType.Wall15, "Wall15");
cubeTypeDict.Add(CubeType.Wall16, "Wall16");
cubeTypeDict.Add(CubeType.Wall17, "Wall17");
cubeTypeDict.Add(CubeType.Wall18, "Wall18");
cubeTypeDict.Add(CubeType.Wall19, "Wall19");
cubeTypeDict.Add(CubeType.Wall20, "Wall20");
cubeTypeDict.Add(CubeType.Wall21, "Wall21");
cubeTypeDict.Add(CubeType.Wall22, "Wall22");
cubeTypeDict.Add(CubeType.Wall23, "Wall23");
cubeTypeDict.Add(CubeType.Wall24, "Wall24");
cubeTypeDict.Add(CubeType.Wall25, "Wall25");
cubeTypeDict.Add(CubeType.Wall26, "Wall26");
cubeTypeDict.Add(CubeType.Wall27, "Wall27");
cubeTypeDict.Add(CubeType.Wall28, "Wall28");
cubeTypeDict.Add(CubeType.Wall29, "Wall29");
}

Yes, you can by,
Image img;
Debug.Log("Image Name: " + img.sprite.name);
Additionally you can change sprite as well by giving Texture2D
img.sprite = Sprite.Create (spr.texture, img.sprite.rect, new Vector2 (0.5f, 0.5f));
Here spr is a Sprite, you can use Texture2D directly.

this.transform.gameobject.GetComponent<Image> ().name;

Related

Using ImageIcon to access a picture, can't access it, how to fix?

I am using ImageIcon to access a photo I have cropped. I put all the cropped pictures in a pic source folder in side the project. Yet when I try to use this.getClass().getResource("image 2.png") to find the image 2.png photo, the code couldn't find it. Is there anyway to fix this, do I have to re upload all the picture into a different folder?
the "image 2.png" is inside the pic source folder, which is within the folder of the project Alle, according to the navigator panel on the right. (I am using eclipse)
Here is my code:
import java.awt.*;
import java.io.File;
import java.io.IOException;
import javax.swing.*;
public class alle extends JFrame {
JButton button1, button2;
JLabel Label1, Label2;
ImageIcon Icon1;
ImageIcon Icon2;
public alle() {
setLayout(new GridLayout(2,3)); //create a gridwolrd like thing that's like 2 row, 3 column
button1 = new JButton("set");
add(button1);
Label1 = new JLabel(" button");
add(Label1);
Icon1 = new ImageIcon(this.getClass().getResource("/alle/pic/image 2.png")) ;
JLabel p = new JLabel(Icon1);
add(p);
}
public static void main (String arg[]) {
alle adfc = new alle();
adfc.setResizable(false);
adfc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
adfc.setVisible(true);
adfc.pack();
File f = new File ("/alle/pic/image 2.png");
System.out.print(f.exists());}}
I also had this kind of problem about a year ago. You should be able to resolve it by writing a loader method for retrieving the images. You can find a detailed answer on this article which also worked for me: Images Won't Appear In A Jar
If you don't want to read through the whole post here's the code sample that should resolve things, you only need to adjust it to your needs:
public ImageIcon loadIcon(String iconName) throws IOException {
ClassLoader loader = this.getClass().getClassLoader();
BufferedImage icon = ImageIO.read(loader.getResourceAsStream(iconName));
return new ImageIcon(icon);
}
From this method you should be able to retrieve your images. I hope this helps you.

JavaFx - Creating animation from Image array

I have array of Image in JavaFx. I want create a clip video (animation) from those images, including sound file.
How Can I achieve this?
NOTE: I want to get a video file at the end of the process (avi, mp4 ...).
This is my array:
Image[] frames
I tried use "keyFrame" class... but without success:
ImageView destImageView = new ImageView();
Group group;
group = new Group();
group.setTranslateX(300);
group.setTranslateY(450);
Image[] frames = m.getFrames();
KeyFrame[] kf = new KeyFrame[frames.length];
for(int i=0;i<frames.length;i++){
kf[0] =new KeyFrame(new Duration(0), new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
// destImageView.setImage();
// group.getChildren().setAll(destImageView);
}
});
}
You can use
javax.imageio.ImageIO.write(javafx.embed.swing.SwingFXUtils.fromFXImage(frame, null), "png", new File(directory, fileName));
to save each image as a png file.
Make sure to give the frames sequentially numbered filenames, e.g. img0000.png, img0001.png etc..
Then use ImageJ/Fiji (https://imagej.net/Fiji/Downloads)
to import the image sequence and save as an avi file. Alternatively, as ImageJ is open-source and written in Java you could import and use directly the ImageJ class
ij.plugin.filter.AVI_Writer
You could then convert it to an mp4 or other format using VLC Player, for example.

Send bitmap parameter to another xaml page

In Windows Phone 8.1 , i have a ListView. My list is populated with an ObservableColection of Pictures. In class Pictures i have pictureName , and bitmapImage.
In ListView_Item_Click , i want to click a Picture and send it to another xaml page.
BitmapImage img = new BitmapImage();
img = ((Picture)e.ClickedItem).Image;//imi selectez imaginea care doresc!!
var image = new Image();
image.Source = img;
Frame.Navigate(typeof(Page2), image); in mainpage.xaml.cs
I wouldn't pass BitmapImage as a parameter of Frame.Navigate - it's not serializable and there will be a problem with SuspensionManager or Resuming/Suspending events.
The solution depends on your images - where to they come from - if it's a file, then you can just pass a path to that file and then in OnNavigated (for example), set the ImageSource from file.
Other method may be to set BitmapImage in target page, before it's navigated to - for example use static property:
public class TargetPage : Page, INotifyPropertyChanged
{
private static BitmapImage bmpImage;
public static BitmapImage BmpImage
{
get { return bmpImage; }
set { bmpImage = value; RaisePropertyChanged("BmpImage"); }
}
// rest of the code
Then you can just set the image before navigating:
TargetPage.BmpImage = img;
Frame.Navigate(typeof(TargetPage));
Also you should remember about Suspending and Resuming events and the case when your app is being terminated while it's Suspended. In every case you should somehow remember the source of the image - using SuspensionManager, PageState, Settings or other method.

AForge.NET Image Color Manipulation

I discovered AForge a few days ago with a goal in mind. I wanted to be able to manipulate an image's colors. However, after trying several different methods I have not been able to find a resolution.
I looked thoroughly through the documentation they give, but it hasn't been any help to me. The specific part of the documentation I have been using is:
http://www.aforgenet.com/framework/docs/html/3aaa490f-8dbe-f179-f64b-eedd0b9d34ac.htm
The example they give:
// create filter
YCbCrLinear filter = new YCbCrLinear( );
// configure the filter
filter.InCb = new Range( -0.276f, 0.163f );
filter.InCr = new Range( -0.202f, 0.500f );
// apply the filter
filter.ApplyInPlace( image );
I replicated it for a button click event, but the 'image' portion of it wasn't specified. I converted the image inside of my picturebox to a bitmap, then referenced it in the last line thinking that it would work. But it had no affect at all.
My code is the following:
private void ColManButton_Click(object sender, EventArgs e)
{
Bitmap newimage = new Bitmap(pictureBox1.Image);
YCbCrLinear filter = new YCbCrLinear();
filter.InCb = new Range(-0.276f, 0.163f);
filter.InCr = new Range(-0.202f, 0.500f);
filter.ApplyInPlace(newimage);
}
My question essentially is, to anyone familiar or willing to help with this framework, how do I take my image and manipulate its color using AForge's YCbCrLinear Class under my button's click event?
Remember to set the picture box image after you have applied the filtering.
private void ColManButton_Click(object sender, EventArgs e)
{
Bitmap newimage = new Bitmap(pictureBox1.Image);
YCbCrLinear filter = new YCbCrLinear();
filter.InCb = new Range(-0.276f, 0.163f);
filter.InCr = new Range(-0.202f, 0.500f);
filter.ApplyInPlace(newimage);
pictureBox1.Image = newimage;
}
at the aforge website you can download the source code of a sample filter application, did you try it ?

Is there a way to create multiple instances of CacheManager in Microsoft Enterprise Library, programatically without depending on configuration file

We are trying to migrate to use Microsoft Enterprise Library - Caching block. However, cache manager initialization seems to be pretty tied to the config file entries and our application creates inmemory "containers" on the fly. Is there anyway by which an instance of cache manager can be instantiated on the fly using pre-configured set of values (inmemory only).
Enterprise Library 5 has a fluent configuration which makes it easy to programmatically configure the blocks. For example:
var builder = new ConfigurationSourceBuilder();
builder.ConfigureCaching()
.ForCacheManagerNamed("MyCache")
.WithOptions
.UseAsDefaultCache()
.StoreInIsolatedStorage("MyStore")
.EncryptUsing.SymmetricEncryptionProviderNamed("MySymmetric");
var configSource = new DictionaryConfigurationSource();
builder.UpdateConfigurationWithReplace(configSource);
EnterpriseLibraryContainer.Current
= EnterpriseLibraryContainer.CreateDefaultContainer(configSource);
Unfortunately, it looks like you need to configure the entire block at once so you wouldn't be able to add CacheManagers on the fly. (When I call ConfigureCaching() twice on the same builder an exception is thrown.) You can create a new ConfigurationSource but then you lose your previous configuration. Perhaps there is a way to retrieve the existing configuration, modify it (e.g. add a new CacheManager) and then replace it? I haven't been able to find a way.
Another approach is to use the Caching classes directly.
The following example uses the Caching classes to instantiate two CacheManager instances and stores them in a static Dictionary. No configuration required since it's not using the container. I'm not sure it's a great idea -- it feels a bit wrong to me. It's pretty rudimentary but hopefully helps.
public static Dictionary<string, CacheManager> caches = new Dictionary<string, CacheManager>();
static void Main(string[] args)
{
IBackingStore backingStore = new NullBackingStore();
ICachingInstrumentationProvider instrProv = new CachingInstrumentationProvider("myInstance", false, false,
new NoPrefixNameFormatter());
Cache cache = new Cache(backingStore, instrProv);
BackgroundScheduler bgScheduler = new BackgroundScheduler(new ExpirationTask(null, instrProv), new ScavengerTask(0,
int.MaxValue, new NullCacheOperation(), instrProv), instrProv);
CacheManager cacheManager = new CacheManager(cache, bgScheduler, new ExpirationPollTimer(int.MaxValue));
cacheManager.Add("test1", "value1");
caches.Add("cache1", cacheManager);
cacheManager = new CacheManager(new Cache(backingStore, instrProv), bgScheduler, new ExpirationPollTimer(int.MaxValue));
cacheManager.Add("test2", "value2");
caches.Add("cache2", cacheManager);
Console.WriteLine(caches["cache1"].GetData("test1"));
Console.WriteLine(caches["cache2"].GetData("test2"));
}
public class NullCacheOperation : ICacheOperations
{
public int Count { get { return 0; } }
public Hashtable CurrentCacheState { get { return new System.Collections.Hashtable(); } }
public void RemoveItemFromCache(string key, CacheItemRemovedReason removalReason) {}
}
If expiration and scavenging policies are the same perhaps it might be better to create one CacheManager and then use some intelligent key names to represent the different "containers". E.g. the key name could be in the format "{container name}:{item key}" (assuming that a colon will not appear in a container or key name).
You can using UnityContainer:
IUnityContainer unityContainer = new UnityContainer();
IContainerConfigurator configurator = new UnityContainerConfigurator(unityContainer);
configurator.ConfigureCache("MyCache1");
IContainerConfigurator configurator2 = new UnityContainerConfigurator(unityContainer);
configurator2.ConfigureCache("MyCache2");
// here you can access both MyCache1 and MyCache2:
var cache1 = unityContainer.Resolve<ICacheManager>("MyCache1");
var cache2 = unityContainer.Resolve<ICacheManager>("MyCache2");
And this is an extension class for IContainerConfigurator:
public static void ConfigureCache(this IContainerConfigurator configurator, string configKey)
{
ConfigurationSourceBuilder builder = new ConfigurationSourceBuilder();
DictionaryConfigurationSource configSource = new DictionaryConfigurationSource();
// simple inmemory cache configuration
builder.ConfigureCaching().ForCacheManagerNamed(configKey).WithOptions.StoreInMemory();
builder.UpdateConfigurationWithReplace(configSource);
EnterpriseLibraryContainer.ConfigureContainer(configurator, configSource);
}
Using this you should manage an static IUnityContainer object and can add new cache, as well as reconfigure existing caching setting anywhere you want.

Resources