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;
...
Related
I'm currently trying to update a program by having a button (nextImg) that when clicked calls a function that creates two bitmaps, generates a number starting from 0, and increments every time the nextImg button gets clicked. Then the program takes that number(num) and returns a string from a List[num] which has all the Uri sources of my photos. However when I click next image I get a
System.NullReferenceException.
private void Image_Loaded(object sender, RoutedEventArgs e)
{
// Create a new BitmapImage.
BitmapImage b = new BitmapImage();
BitmapImage b1 = new BitmapImage();
Image img = new Image();
string sSrc = numGen();
b.BeginInit();
b.UriSource = new Uri(sSrc);
b.EndInit();
b1.BeginInit();
b1.UriSource = b.UriSource;
b1.EndInit();
img = sender as Image;
img.Source = b; // Error on this line
Canvas1.Height = Picturebox1.Height;
Canvas1.Width = Picturebox1.Width;
Canvas1.Margin = Picturebox1.Margin;
}
I want to know the image name whenever image clicked.
i used this code for adding image to button background:
ImageBrush brush1 = new ImageBrush();
brush1.ImageSource = new BitmapImage(new Uri("ms-appx:///Assets/emptyseat.jpg"));
btn.Background = brush1;
now i want to know the name of the image whenever image clicked. please any one help me out.
You can save the name in the buttons Tag property
ImageBrush brush1 = new ImageBrush();
brush1.ImageSource = new BitmapImage(new Uri("ms-appx:///Assets/emptyseat.jpg"));
btn.Tag = "emptyseat.jpg";
btn.Background = brush1;
public void OnClick(object sender, RoutedEventArgs e)
{
var btn = (Button)sender;
string name = btn.Tag;
}
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);
I am working with WPF and I have an application that the user loads an image file into a RichTextBox and they can rotate the image and print it. I am not sure as to why the image after it has been rotated will not print as it is displayed on the screen. Instead it prints the original. I am new to this so any help would be greatly appreciated!
The following is the code for my application. Code when the retrieve file Button is clicked:
private void retrieve_button_Click(object sender, RoutedEventArgs e)
{
//Retrieve the file or image you are looking for
OpenFileDialog of = new OpenFileDialog();
of.Filter = "Formats|*.jpg;*.png;*.bmp;*.gif;*.ico;*.txt|JPG Image|*.jpg|BMP image|*.bmp|PNG image|*.png|GIF Image|*.gif|Icon|*.ico|Text File|*.txt";
var dialogResult = of.ShowDialog();
if (dialogResult == System.Windows.Forms.DialogResult.OK)
{
try
{
System.Windows.Controls.RichTextBox myRTB = new System.Windows.Controls.RichTextBox();
{
Run myRun = new Run();
System.Windows.Controls.Image MyImage = new System.Windows.Controls.Image();
MyImage.Source = new BitmapImage(new Uri(of.FileName, UriKind.RelativeOrAbsolute));
InlineUIContainer MyUI = new InlineUIContainer();
MyUI.Child = MyImage;
rotateright_button.IsEnabled = true;
print_button.IsEnabled = true;
Paragraph paragraph = new Paragraph();
paragraph.Inlines.Add(myRun);
paragraph.Inlines.Add(MyUI);
FlowDocument document = new FlowDocument(paragraph);
richTextBox.Document = document;
}
}
catch (ArgumentException)
{
System.Windows.Forms.MessageBox.Show("Invalid File");
}
}
}
When the rotate right button is clicked the following code is executed:
RotateTransform cwRotateTransform;
private void rotateright_button_Click(object sender, RoutedEventArgs e)
{
richTextBox.LayoutTransform = cwRotateTransform;
if (cwRotateTransform == null)
{
cwRotateTransform = new RotateTransform();
}
if (cwRotateTransform.Angle == 360)
{
cwRotateTransform.Angle = 0;
}
else
{
cwRotateTransform.Angle += 90;
}
}
After the Image has been loaded and rotated the user can use the following code to print:
private void InvokePrint(object sender, RoutedEventArgs e)
{
System.Windows.Controls.PrintDialog printDialog = new System.Windows.Controls.PrintDialog();
if ((bool)printDialog.ShowDialog().GetValueOrDefault())
{
FlowDocument flowDocument = new FlowDocument();
flowDocument = richTextBox.Document;
flowDocument.ColumnWidth = printDialog.PrintableAreaWidth;
flowDocument.PagePadding = new Thickness(65);
IDocumentPaginatorSource iDocPag = flowDocument;
printDialog.PrintDocument(iDocPag.DocumentPaginator, "Print Document");
}
}
Try this (substitute yourImageControl in the first line, specify which RotateFlipType you want and be sure to reference the System.Drawing dll):
System.Drawing.Bitmap bitmap = BitmapSourceToBitmap((BitmapSource)YourImageControl.Source);
bitmap.RotateFlip(System.Drawing.RotateFlipType.Rotate90FlipNone);
public static System.Drawing.Bitmap BitmapSourceToBitmap(BitmapSource bitmapsource)
{
System.Drawing.Bitmap bitmap;
using (MemoryStream outStream = new MemoryStream())
{
BitmapEncoder enc = new BmpBitmapEncoder();
enc.Frames.Add(BitmapFrame.Create(bitmapsource));
enc.Save(outStream);
bitmap = new System.Drawing.Bitmap(outStream);
}
return bitmap;
}
Another option for conversion...
P.S. You would get a better answer in less time if you posted some code and told us more about what you have tried.
So I have an image that when the user clicks on a button it will change it to a new item. However, whenever the user clicks on one of the button, the window will go blank. How can I get this to work? Thank you.
private void Next_Click(object sender, RoutedEventArgs e)
{
if (imageNumber > 6)
{
imageNumber = 1;
}
imageNumber++;
string sUri = string.Format("#/Resources/{0}", imageSource[imageNumber]);
Uri src = new Uri(sUri, UriKind.Relative);
var bmp = new BitmapImage(src);
img.Source = bmp;
}
xaml
<Image x:Name="img">
<Image.Source>
<BitmapImage UriSource="Resources/BlackJackTut-1.jpg" />
</Image.Source>
</Image>
In WPF application you can do same also with "pack://application:,,,/resources/imagename.png".
This way called Pack URI. This is static, but with these code you can do same an even use resource ;)
Put image in Resources.
private BitmapImage ConvertBitmapToBitmapImage(System.Drawing.Bitmap bitmap)
{
MemoryStream memoryStream = new MemoryStream();
bitmap.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = new MemoryStream(memoryStream.ToArray());
bitmapImage.EndInit();
return bitmapImage;
}
and then use this:
private void btn_Click(object sender, RoutedEventArgs e)
{
this.Img.Source = ConvertBitmapToBitmapImage(Properties.Resources.iamge1);
}
Just make an Image outside the mainWindow.
Then:
myImageOnScreen.Source = myImageOffScreen.Source;