Setting the value of an image using a String - c#

I have a string, imgchng, and twenty images, image1, image2, image3,etc.
The value of imgchng is always the name of one of the images.How would I set the value of the the current image that imgchng is referring to? For instance, the user sets the value of imgchng as image12. How would I tell image12's source to change? imgchng.Source = (source goes here); doesn't work, because that would set the property of the string, not the image.I know how to set the source of the image, just not how to set the source of whatever image the string is referring to.My intention is to avoid a humongous if statement that would be over 1000 lines long, like the example one below:
if (textBlock2.Text == "First User Selection")
{
if (imgchng == "image1")
{
BitmapImage bmp = new BitmapImage(new Uri("/Images/FirstImg.png"));
image1.Source = bmp;
}
else if (imgchng == "image2")
{
BitmapImage bmp = new BitmapImage(new Uri("/Images/FirstImg.png"));
image2.Source = bmp;
}
//Continue this for all 20 images
}
else if (textBlock2.Text == "Second User Selection")
{
if (imgchng == "image1")
{
BitmapImage bmp = new BitmapImage(new Uri("/Images/SecondImg.png"));
image1.Source = bmp;
}
else if (imgchng == "image2")
{
BitmapImage bmp = new BitmapImage(new Uri("/Images/SecondImg.png"));
image2.Source = bmp;
}
//Continue this for all 20 images
}
else if (textBlock2.Text == "Third User Selection")
{
if (imgchng == "image1")
{
BitmapImage bmp = new BitmapImage(new Uri("/Images/ThirdImg.png"));
image1.Source = bmp;
}
else if (imgchng == "image2")
{
BitmapImage bmp = new BitmapImage(new Uri("/Images/ThirdImg.png"));
image2.Source = bmp;
}
//Continue this for all 20 images
}
else if (textBlock2.Text == "Fourth User Selection")
{
if (imgchng == "image1")
{
BitmapImage bmp = new BitmapImage(new Uri("/Images/FourthImg.png"));
image1.Source = bmp;
}
else if (imgchng == "image2")
{
BitmapImage bmp = new BitmapImage(new Uri("/Images/FourthImg.png"));
image2.Source = bmp;
}
//Continue this for all 20 images
}
Basically what I'm trying to do is, as #ctacke said, given the string 'image1', how do I get the instance of the control named 'image1'?

Declare a BitmapImage array of 20 images. Bind that to the UI. Take the user selection as an integer. Access the image from array using index (obviously user input -1).Change the source of that image. Does this resolve your problem?

To set the source of the image you have to do the following.
BitmapImage bmp=new BitmapImage(new Uri("your image name will go here"));
image.Source=bmp;
Hope this helps.

Related

How to load file image.tif into picturebox?

I want to load an image file .tif (that image has many classes of colors to indicate the height) into my picture box, when I use load image 2D as normal, it is displayed as a black image.
private static Bitmap ResizeImage(Bitmap image, Size newSize)
{
Bitmap newImage = new Bitmap(newSize.Width, newSize.Height);
using (Graphics GFX = Graphics.FromImage((Bitmap)newImage))
{
GFX.DrawImage(image, new Rectangle(Point.Empty, newSize));
}
return newImage;
}
private void loadImage_Click(object sender, EventArgs e)
{
OpenFileDialog opf = new OpenFileDialog();
opf.Title = "Select Image";
opf.Filter = "Image File (*.jpg; *.TIF; *.jpeg)| *.jpg; *.TIF; *.jpeg ";
if (opf.ShowDialog()==DialogResult.OK)
{
Bitmap image = new Bitmap(opf.FileName);
Image Image = ResizeImage(image, mapViewer.Size);
mapViewer.Image = Image;
Origin = Image;
width = Origin.Width;
height = Origin.Height;
}
}

how to clear memory of WritableBitmap

I am picking image from gallery using PhotoChooserTask, here is my setup
private void ApplicationBarIconButton_Click(object sender, EventArgs e)
{
PhotoChooserTask photo = new PhotoChooserTask();
photo.Completed += photo_Completed;
photo.Show();
}
void photo_Completed(object sender, PhotoResult e)
{
if (e.ChosenPhoto != null)
{
WriteableBitmap wbmp1 = PictureDecoder.DecodeJpeg(e.ChoosenPhoto, (int)scrnWidth, (int)scrnHeight);
ImageBrush iBru = new ImageBrush();
iBru.ImageSource = wbmp1;
iBru.Stretch = Stretch.Fill;
ContentPanel.Background = iBru;
}
}
Problem: with this way is it works only with .JPEG images
to make it work with other image formats, I tried this:
void photo_Completed(object sender, PhotoResult e)
{
if (e.ChosenPhoto != null)
{
WriteableBitmap wBmp = new WriteableBitmap(0, 0);//6930432
wBmp.SetSource(e.ChosenPhoto);//23105536
MemoryStream tmpStream = new MemoryStream();//23105536
wBmp.SaveJpeg(tmpStream, (int)scrnWidth, (int)scrnHeight, 0, 100);//22831104
tmpStream.Seek(0, SeekOrigin.Begin);//22831104
WriteableBitmap wbmp1 = PictureDecoder.DecodeJpeg(tmpStream, (int)scrnWidth, (int)scrnHeight);//24449024
ImageBrush iBru = new ImageBrush();//24449024
iBru.ImageSource = wbmp1;//24449024
iBru.Stretch = Stretch.Fill;
ContentPanel.Background = iBru;
}
}
this way works with different image formats, but it is not memory efficient.
I have mentioned number of bytes used after each line for better understanding.
Question: In latter code snippet, I do not need wbmp anymore, How can I clear memory used by wbmp object?
As #Soonts suggested I used BitmapImage and it solves my purpose,
BitmapImage bmp = new BitmapImage();
bmp.DecodePixelWidth = (int)scrnWidth;
bmp.DecodePixelHeight = (int)scrnHeight;
bmp.SetSource(e.ChosenPhoto);
It consumes less memory and we can scale down image
Get rid of the WriteableBitmap, instead do something like this:
var bmp = new System.Windows.Media.Imaging.BitmapImage();
bmp.SetSource( e.ChosenPhoto );
var ib = new ImageBrush() { ImageSource = bmp, Stretch = Stretch.Fill };
ContentPanel.Background = ib;

Display image from Uri

In a Windows Phone 8 app, I'm using the following code to display an image:
InitializeComponent();
Image i = new Image();
i.Source = new BitmapImage(new Uri("C:\\Data\\Users\\Public\\Pictures\\Sample Pictures\\sample_photo_05.jpg", UriKind.RelativeOrAbsolute));
LayoutRoot.Children.Add(i);
But when the page loads, the screen is empty. Can anyone see what I'm doing wrong?
Copy your image in Asset folder and set Build Action==Content
Image i = new Image();
i.Source = new BitmapImage(new Uri("/yourProjectName;component/Assets/YourImageName", UriKind.RelativeOrAbsolute));
LayoutRoot.Children.Add(i);
Follow below steps to set image by URI in windows phone
1. Copy Image in Images folder in your solution.
2. Set Image as Resource Rightclick on Image->Properties->Build Action ==Content
InitializeComponent();
Image i = new Image();
i.Height =100;
i.Width=100;
i.Source = new BitmapImage(new Uri("/Images/YourImageName", UriKind.RelativeOrAbsolute));
LayoutRoot.Children.Add(i);
Using a CameraCaptureTask, May be like this
initialize a CameraCaptureTask Object
CameraCaptureTask cameracapturetask = new CameraCaptureTask();
cameracapturetask.Completed += new EventHandler<PhotoResult>(cameracapturetask_Completed);
cameracapturetask.Show();
and inside its event
void cameracapturetask_Completed(object sender, PhotoResult e)
{
try
{
if (e.TaskResult == TaskResult.OK)
{
BitmapImage bmp = new BitmapImage();
bmp.SetSource(e.ChosenPhoto);
img.Source = bmp;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Copy your image in a folder (Images) and set Build Action==Content
// draw an image, set relative source (in project) and add to LayoutRoot.
var i = new Image{
Source = new BitmapImage(
new Uri("/project;component/Images/image.jpg", UriKind.Relative))
};
LayoutRoot.Children.Add(i);

display an image from file in wpf isn't working?

I have a button b3 and an image named pictureBox1 . Im using WPF, however I'm using the winforms openFileDialog instead of the one that comes with WPF :
below is the code that I put inside the click event of my button :
private void b3_Click(object sender, RoutedEventArgs e)
{
System.Windows.Forms.OpenFileDialog openDialogIcon = new System.Windows.Forms.OpenFileDialog();
if (openDialogIcon.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
Image i = new Image();
BitmapImage src = new BitmapImage();
src.BeginInit();
src.UriSource = new Uri(openDialogIcon.FileName, UriKind.Absolute);
src.CacheOption = BitmapCacheOption.OnLoad;
src.EndInit();
i.Source = src;
i.Stretch = Stretch.Uniform;
//int q = src.PixelHeight; // Image loads here
}
}
When I click the button and select an icon. The icon doesn't appear in the pictureBox1.
Can someone please explain why the code above doesn't show the icon inside the pictureBox?
You need to assign your image to the pictureBox, else you wont see it on your screen and you only made the image object in memory.
private void b3_Click(object sender, RoutedEventArgs e)
{
System.Windows.Forms.OpenFileDialog openDialogIcon = new System.Windows.Forms.OpenFileDialog();
if (openDialogIcon.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
BitmapImage src = new BitmapImage();
src.BeginInit();
src.UriSource = new Uri(openDialogIcon.FileName, UriKind.Absolute);
src.CacheOption = BitmapCacheOption.OnLoad;
src.EndInit();
pictureBox1.Source = src;
}
}
Try to drag and drop a Image control in your window
...
//imageStretch <- the name of Image control
i.Stretch = Stretch.Uniform;
//int q = src.PixelHeight; // Image loads here
imageStretch.Source = src;
...

Databinding Image Source at Windows Phone

I have datatable with byte[] column.
On app page I iterate through rows and bind column data to SourceProperty
for (i = 0;..) {
Image img = new Image();
img.Width = 100;
img.Height = 100;
Binding bnd = new Binding("Fields[" + i + "].Blob"); // Blob column
bnd.Converter = new ByteToImageConverter();
img.SetBinding(Image.SourceProperty, bnd);
}
Near every image I have button that calls CameraCaptureTask camTask.
Before camtask.Show() I assign current Image to global pointer _imgCurrent = img
camtask.Completed += (s, e)
{
if (e.TaskResult != TaskResult.OK) return;
BitmapImage bmp = new BitmapImage();
bmp.SetSource(e.ChosenPhoto);
_imgCurrent.Source = bmp;
_imgCurrent.SetValue(Image.SourceProperty, bmp);
}
But in that case DataContext don't update. I suppose that I need to implement INotifyPropertyChanged? I need to inherit Image from that interface or I can trigger it in time of Source update?
The problem was at Binding creation: I forgot about Mode=TwoWay.

Categories