I'm trying to modify an example of a machine vision library ( Common Vision Blox ). The example gets an image from the camera, converts it to bitmap format and shows to the user through a PictureBox.
What I want it is to save the data to a file, but I get a BMP all in black. I tried some variants but the result is the same.
If I save the image from the PictureBox, using PictureBox.SaveImage the result is the same if I
The code is this :
private Bitmap _bm;
private bool _bRequiresCopy;
private void cvImg_ImageUpdated(object sender, EventArgs e)
{
// create a bitmap out of the image
_bm = CvbImageToBitmap(cvImg.Image, out _bRequiresCopy);
// and display it in the picture box
//if (_bm != null) // Only this two lines in the original code
// pictureBox.Image = _bm;
if (_bm != null)
{
pictureBox.Image = _bm;
// Tests to save
_bm.Save("c:\\test.bmp"); // This don't work
// The following don't work, but if add a button to the form and just place in the click
// event of the button it stores the image correctly
pictureBox.Image.Save("c:\\test2.bmp");
Bitmap TestBMP = new Bitmap(_bm);
TestBMP.Save("c:\\test3.bmp"); // This don't work
}
// setup grab checkbox
chkGrab.Enabled = cvImg.IsGrabber;
chkGrab.Checked = false;
}
Related
I am currently following walk through on how to upload an image from a phone's photo gallery link here to allow users to upload an image to my application which they can set as their profile picture. I am able to get an image from the phone's gallery however when I try and save, I cannot retrieve the image source from the xaml.
Below is the xaml for the image and the button that the user clicks on to upload an image.
<Button Text="Pick a Profile Image"
Clicked="OnPictureButton_Clicked"
Grid.Column="0"></Button>
<Image Source="{Binding Employee.ProfilePicture}"
Grid.Column="1"
x:Name="profilePicture"
Grid.RowSpan="2"
WidthRequest="200"></Image>
Here is the corresponding c# code:
private async void OnPictureButton_Clicked(object sender, EventArgs e)
{
(sender as Button).IsEnabled = false;
// _stream is a private global variable
// Allow the user to view images on the phone
_stream = await DependencyService.Get<IPhotoPickerService>().GetImageStreamASync();
// If they select an image, set it as the source for profilePicture
if (_stream != null)
{
profilePicture.Source = ImageSource.FromStream(() => _stream);
}
(sender as Button).IsEnabled = true;
}
private async void Clicked_EmployeeSaved(object sender, EventArgs e)
{
var data = (EmployeeFull)BindingContext;
var uploadedPicture = profilePicture.Source; // Should be uploaded image
// Testing how to get the source for the image (Can disregard for question)
Bitmap bitmap = BitmapFactory.DecodeStream(_stream);
MemoryStream ms = new MemoryStream();
bitmap.Compress(Bitmap.CompressFormat.Jpeg, 100, ms);
byte[] byteArray;
byteArray = ms.ToArray();
}
Now, I know that once the user selects an image from the gallery on their device, the stream closes and so I will be unable to access it later on in the code, like I have tried in the second function shown.
However, I am unable to retrieve the name of the image that I have selected to upload. On the screen, the user is able to see this image as I have set the selected image as the source for the profilePicture tag but when I try and get that source when the user clicks 'save', it shows an ImageSource` object, not a string which I expected.
Is there another way I can get the uploaded image's name?
If you are sure the ImageSource is a stream, you can use a cast ImageSource to StreamImageSource, and then get the stream from that.
Example:
var imageStreamSource = (StreamImageSource)profilePicture.Source;
Stream actualStream = await imageStreamSource.Stream(new CancellationToken());
I'm trying to do something when I click image displayed inside pictureBox1.
pictureBox is loaded with this code:
string imgpath = #"img\256.png";
pictureBox48.Image = Image.FromFile(imgpath);
Then control is released to me so I can see that the picture loaded correctly.
Then i click the picture:
public void pictureBox48_Click(object sender, EventArgs e)
{
string variable1 = pictureBox48.ImageLocation;
Form3 fo = new Form3(variable1);
fo.ShowDialog();
}
This doesn't work. When I debug the code I see that variable1 stay null, that is pictureBox48.ImageLocation is null. Why is that? Shouldn't it be the path to the image that is assigned there?
You can't get the image path when you set the image using the Image property because you are assigning an Image object which can come from different sources.
Set the image using ImageLocation.
string imgpath = #"img\256.png";
pictureBox48.ImageLocation = imgpath;
When you click in the PictureBox you can get the path using the same property:
public void pictureBox48_Click(object sender, EventArgs e)
{
string variable1 = pictureBox48.ImageLocation;
Form3 fo = new Form3(variable1);
fo.ShowDialog();
}
When dealing with Image or PictureBox I would recommend to not use something like Location or Path of the image. Assume that when the image is loaded user removes it from the hard drive and you're left with the code full of errors.
That's why you should rely on Image itself as it contains every information about the image like pixel format, width, height and raw pixel data.
I would recommend you to just copy the image, not the path to the file.
This piece of code should give you a hint:
pixtureBox48.Image = Image.FromFile(imgPath);
// above code assumes that the image is still on hard drive and is accessible,
// now let's assume user deletes that file. You have the data but not on the physical location.
Image copyImage = (Image)pictureBox48.Image.Clone();
Form3 fo = new Form(copyImage); // change .ctor definition to Form(Image copy)
fo.ShowDialog();
I inserted an image into a button, but it's not a background image. So, I want to resize the image's size that I want for example Height=30, Width=20 or sometime Height=50, Width=50. Some people told me that it's impossible to resize an image in a button if I inserted it as an background image it's possible.However, if I insist to resize the image it's possible? I don't believe that nobody can do it.
Is this a windows form application? Forgive me if I didn't understand the question, but you should be able to resize the image from the Properties menu if you're using a PictureBox. Right-click on the image you inserted, select properties and there should be a field for Size.
I am assuming that you want to resize the image present inside the button. You can try something like this in Designer File:
//
// ButtonName
//
this.ButtonName.AutoSize = false;
Image image = new Bitmap(customWidth,customHeight);
using (Graphics g = Graphics.FromImage(image )) {
g.DrawImage(FileLocation, 0, 0, customWidth, customHeight);
}
this.ButtonName.Image = image;
This will help in button resizing but the picture won't be clear if much increased.
Button.Resize += Button_Resize;
private void Button_Resize(object sender, EventArgs e)
{
Button button = (Button)sender;
Image resize = new Bitmap(button.Image,new Size(button.Image.Width, (button.Height - 13))); button.Image = resize;
}
I'm working on making a memory game in C# using borders with images and changing their source. Changing the first source works correctly, however after the second flip I call a function to check if they match before flipping them to a correct or hidden image. I've been having an issue with the second image because the events I've tried call before the function has exited so the second image only shows the hidden image. Is there an event I can call to check if the image has loaded before exiting?
//border mousedown event
private void Border_MouseDown(object sender, MouseButtonEventArgs e)
{
//flip the image over
Border picBorder = sender as Border;
picBorder.IsEnabled = false; //disable the border so they can't click it again
int index = Int32.Parse(Regex.Match(picBorder.Name, #"\d+").Value); //get border #
BitmapImage car = new BitmapImage(carImages[index]);
picBorder.Child = new Image { Source = car};
picBorder.IsEnabled = false;
//Storing border locations for checking later
borderLocations[currentCarsFlipped] = picBorder;
currentCarsFlipped++;
}
Once CurrentCarsFlipped == 2, I need to check if there is a match. Currently, I have checkWin() which looks like it will work, but is hard to debug when you can only see 1 image for every 2 clicks
use a Boolean variable ,
False if not updated and true after update, then you can check
I am working on a project in c# using windows forms.
me and the group I am in want to make it so that when the user hovers their mouse over an image, in our case a card, that a larger image of that card appears next to the mouse arrow, much in the same way a tool tip would work.
I don't think you can use a tool tip to do this i have tried looking everywhere,
any advice or examples would be great thank you very much
You may want to look at this Code Project Article
It shows you how to create an OwnerDrawn ToolTip with an Image.
Thanks for the responses I got everything figured out.
What I wanted to do was that when I moused over a certain area a different image for that area would popup in the same way that a tool tip did. So after some research I figured out how to create my own tool tip class.
here's an example.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
CustomToolTip tip = new CustomToolTip();
tip.SetToolTip(button1, "text");
tip.SetToolTip(button2, "writing");
button1.Tag = Properties.Resources.pelican; // pull image from the resources file
button2.Tag = Properties.Resources.pelican2;
}
}
class CustomToolTip : ToolTip
{
public CustomToolTip()
{
this.OwnerDraw = true;
this.Popup += new PopupEventHandler(this.OnPopup);
this.Draw +=new DrawToolTipEventHandler(this.OnDraw);
}
private void OnPopup(object sender, PopupEventArgs e) // use this event to set the size of the tool tip
{
e.ToolTipSize = new Size(600, 1000);
}
private void OnDraw(object sender, DrawToolTipEventArgs e) // use this to customzie the tool tip
{
Graphics g = e.Graphics;
// to set the tag for each button or object
Control parent = e.AssociatedControl;
Image pelican = parent.Tag as Image;
//create your own custom brush to fill the background with the image
TextureBrush b = new TextureBrush(new Bitmap(pelican));// get the image from Tag
g.FillRectangle(b, e.Bounds);
b.Dispose();
}
}
}
A simple way to do is to hide/show a picture box at specified location. Another method is to load & draw (paint) an image using GDI API.