How to move an image from a canvas to another - c#

I'm trying to do a image processing software and for the moment I'm stuck on this.
I've open an image in my first canvas, and now I want to take the image and add some filters on it. Contrast, saturation and so on, and then to see it on the second canvas.
But my main problem is that I can't find out how to use pixels from my first canvas and manipulate them. How can I do this the easy way?
Thanks.
private void openImage_OnClick(object sender, RoutedEventArgs e)
{
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.InitialDirectory = "c:\\";
dlg.DefaultExt = ".jpeg";
dlg.Filter = "Image files (*.png;*.jpg;*.jpeg)|*.png;*.jpg;*.jpeg";
Nullable<bool> result = dlg.ShowDialog();
if (result == true)
{
string filename = dlg.FileName;
ImageBrush brush = new ImageBrush();
brush.ImageSource = new BitmapImage(new Uri(#filename, UriKind.Relative));
CanvasOriginalImage.Background = brush;
}
}

Related

How to load a image into a picture box?

I Am programming in C# and I Already have it that I can select my files from my phone with this text:
private void Button_Click_2(object sender, RoutedEventArgs e)
{
var FileOpenPicker = new Windows.Storage.Pickers.FileOpenPicker();
FileOpenPicker.ViewMode = Windows.Storage.Pickers.PickerViewMode.List;
FileOpenPicker.FileTypeFilter.Add(".jpg");
FileOpenPicker.FileTypeFilter.Add(".jpeg");
FileOpenPicker.FileTypeFilter.Add(".png");
FileOpenPicker.PickSingleFileAndContinue();
How can I let the picture popup in the picturebox?
Use a FileDialog and load a bitmap from the selected path:
using (OpenFileDialog dlg = new OpenFileDialog())
{
dlg.Title = "Open Image";
dlg.Filter = "bmp files (*.bmp)|*.bmp";
if (dlg.ShowDialog() == DialogResult.OK)
{
// Create a new Bitmap object from the picture file on disk,
// and assign that to the PictureBox.Image property
PictureBox1.Image = new Bitmap(dlg.FileName);
}
}

C# Crop Image based on unifrom color?

Is there a way to crops image by removing borders of uniform color ?
EDIT: Based on the similar topic, I tried to use AForge.NET package - Extract biggest blob, it dint work.
I have tried using all the Blob extract functions and All I get is just the channel extract, I need to perform this after I do the Green channel extract. I even tried the extract Black just in case. That too dint work.
Here is my code that I have tried.
private void openToolStripMenuItem_Click(object sender, EventArgs e)// File-Open - Import image and show in PictureBox1
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Title = "Open Image";
dlg.Filter = "bmp files (*.bmp)|*.bmp";
if (dlg.ShowDialog() == DialogResult.OK)
{
ClearCurrentImage();
pictureBox1.Image = System.Drawing.Image.FromFile(dlg.FileName);
////------ AForge.NET sequence------//
//// create filter
//SaturationCorrection filter = new SaturationCorrection(-0.5f);
//// apply the filter
//filter.ApplyInPlace(image);
////------ AForge.NET sequence------//
// Pass original image to the channel extract filter
Bitmap image = new Bitmap(pictureBox1.Image);
//Create filters
ExtractChannel GreenChannel = new ExtractChannel(RGB.G);
//ExtractBiggestBlob BlobGreenChannel = new ExtractBiggestBlob();
//RotateBilinear RotateFilter = new RotateBilinear(30, true);
Shrink Blackfilter = new Shrink(Color.Black);
// ---------------- Apply filter ------------------//
Bitmap ChannelImage = GreenChannel.Apply(image);
//Bitmap BlobChannelImage = BlobGreenChannel.Apply(image);
//Bitmap RBCImage = RotateFilter.Apply(BlobGreenChannel.Apply(GreenChannel.Apply(image)));
//Bitmap BlobChannelImage = BlobGreenChannel.Apply(GreenChannel.Apply(image));
//Bitmap BRBCImage = Blackfilter.Apply(RotateFilter.Apply(BlobGreenChannel.Apply(GreenChannel.Apply(image))));
Bitmap BlackImage = Blackfilter.Apply(GreenChannel.Apply(image));
pictureBox2.Image = BlackImage;
}
dlg.Dispose();
}
I would like to fit the image in the smallest rectangle possible by cropping away all the black background. How can I achieve this?

Confusion about OpenFileDialog

I am following this tutorial and downloaded source code to practice, and it works. The problem occurs when I rewrite the code: just one image is added instead of all the selected images. What am I doing wrong here?
private void button1_Click(object sender, EventArgs e)
{
ofd.Filter = "Images (*.BMP;*.JPG;*.GIF,*.PNG,*.TIFF)|*.BMP;*.JPG;*.GIF;*.PNG;*.TIFF|" +
"All files (*.*)|*.*";
ofd.Multiselect = true;
if (ofd.ShowDialog() == DialogResult.OK)
{
foreach (string name in ofd.FileNames)
{
PictureBox imageControl = new PictureBox();
imageControl.Width = 100;
imageControl.Height = 100;
Image.GetThumbnailImageAbort CallBck = new Image.GetThumbnailImageAbort(ThumbnailCallback);
Bitmap myBitmap = new Bitmap(name);
Image img = myBitmap.GetThumbnailImage(97, 97, CallBck, IntPtr.Zero);
imageControl.Image = img;
panel1.Controls.Add(imageControl);
}
}
}
I'll bet they are all being added but they're just all going on top of each other at location (0,0) in the panel (you should step through your code to check this though).
The solution: Either manually specify a location for each new PictureBox, or use a layout control such as a FlowLayoutPanel.

Windows Forms PictureBox - how to display the image in a certain area of the form

I'm using the following code to open and display image in one of my forms using fileDialog :
private void btnExplorer_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "c:\\";
openFileDialog1.Filter = "All files (*.*)|*.*";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
PictureBox PictureBox1 = new PictureBox();
PictureBox1.Image = new Bitmap(openFileDialog1.FileName);
// Add the new control to its parent's controls collection
this.Controls.Add(PictureBox1);
}
catch (Exception ex)
{
MessageBox.Show("Error loading image" + ex.Message);
}
}
}
The problem is that my image is shown at the top left corner of my form, when I have left almost quarter of my down-right side for this purpose. How can I show it there?
Like I said in my comment, here's how: How to: Position Controls on Windows Forms.
PictureBox PictureBox1 = new PictureBox();
PictureBox1.Image = new Bitmap(openFileDialog1.FileName);
PictureBox1.Location = new Point(20, 100); //20 from left and 100 from top
this.Controls.Add(PictureBox1);
Or change it afterwards:
PictureBox1.Top += 50; //increase distance from top with 50
You can set the location property of the PictureBox before adding it to the Parent.

Load a bitmap image into Windows Forms using open file dialog

I need to open the bitmap image in the window form using open file dialog (I will load it from drive). The image should fit in the picture box.
Here is the code I tried:
private void button1_Click(object sender, EventArgs e)
{
var dialog = new OpenFileDialog();
dialog.Title = "Open Image";
dialog.Filter = "bmp files (*.bmp)|*.bmp";
if (dialog.ShowDialog() == DialogResult.OK)
{
var PictureBox1 = new PictureBox();
PictureBox1.Image(dialog.FileName);
}
dialog.Dispose();
}
You have to create an instance of the Bitmap class, using the constructor overload that loads an image from a file on disk. As your code is written now, you're trying to use the PictureBox.Image property as if it were a method.
Change your code to look like this (also taking advantage of the using statement to ensure proper disposal, rather than manually calling the Dispose method):
private void button1_Click(object sender, EventArgs e)
{
// Wrap the creation of the OpenFileDialog instance in a using statement,
// rather than manually calling the Dispose method to ensure proper disposal
using (OpenFileDialog dlg = new OpenFileDialog())
{
dlg.Title = "Open Image";
dlg.Filter = "bmp files (*.bmp)|*.bmp";
if (dlg.ShowDialog() == DialogResult.OK)
{
PictureBox PictureBox1 = new PictureBox();
// Create a new Bitmap object from the picture file on disk,
// and assign that to the PictureBox.Image property
PictureBox1.Image = new Bitmap(dlg.FileName);
}
}
}
Of course, that's not going to display the image anywhere on your form because the picture box control that you've created hasn't been added to the form. You need to add the new picture box control that you've just created to the form's Controls collection using the Add method. Note the line added to the above code here:
private void button1_Click(object sender, EventArgs e)
{
using (OpenFileDialog dlg = new OpenFileDialog())
{
dlg.Title = "Open Image";
dlg.Filter = "bmp files (*.bmp)|*.bmp";
if (dlg.ShowDialog() == DialogResult.OK)
{
PictureBox PictureBox1 = new PictureBox();
PictureBox1.Image = new Bitmap(dlg.FileName);
// Add the new control to its parent's controls collection
this.Controls.Add(PictureBox1);
}
}
}
Works Fine.
Try this,
private void addImageButton_Click(object sender, EventArgs e)
{
OpenFileDialog of = new OpenFileDialog();
//For any other formats
of.Filter = "Image Files (*.bmp;*.jpg;*.jpeg,*.png)|*.BMP;*.JPG;*.JPEG;*.PNG";
if (of.ShowDialog() == DialogResult.OK)
{
pictureBox1.ImageLocation = of.FileName;
}
}
You should try to:
Create the picturebox visually in form (it's easier)
Set Dock property of picturebox to Fill (if you want image to fill form)
Set SizeMode of picturebox to StretchImage
Finally:
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Title = "Open Image";
dlg.Filter = "bmp files (*.bmp)|*.bmp";
if (dlg.ShowDialog() == DialogResult.OK)
{
PictureBox1.Image = Image.FromFile(dlg.Filename);
}
dlg.Dispose();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
if (open.ShowDialog() == DialogResult.OK)
pictureBox1.Image = Bitmap.FromFile(open.FileName);
}
You, can also try like this, PictureBox1.Image = Image.FromFile("<your ImagePath>" or <Dialog box result>);
PictureBox.Image is a property, not a method. You can set it like this:
PictureBox1.Image = System.Drawing.Image.FromFile(dlg.FileName);
You can try the following:
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog fDialog = new OpenFileDialog();
fDialog.Title = "Select file to be upload";
fDialog.Filter = "All Files|*.*";
// fDialog.Filter = "PDF Files|*.pdf";
if (fDialog.ShowDialog() == DialogResult.OK)
{
textBox1.Text = fDialog.FileName.ToString();
}
}
It's simple. Just add:
PictureBox1.BackgroundImageLayout = ImageLayout.Zoom;

Categories

Resources