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?
Related
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;
}
}
int countimages = 0;
private void timer1_Tick(object sender, EventArgs e)
{
Image img = sc.CaptureWindowToMemory(windowHandle);
Bitmap bmp = new Bitmap(img,img.Width,img.Height);
bmp.Save(#"e:\screenshotsofpicturebox1\screenshot" + countimages + ".bmp");
bmp.Dispose();
string[] images = Directory.GetFiles(#"e:\screenshotsofpicturebox1\", "*.bmp");
if (images.Length > 0)
{
if (pictureBox1.Image != null)
{
File.Delete(images[0]);
countimages = 0;
pictureBox1.Image.Dispose();
}
pictureBox1.Image = Image.FromFile(images[countimages]);
}
countimages += 1;
label1.Text = countimages.ToString();
}
I want to save image to the hard disk
load the image to the pictureBox1
after the image loaded to pictureBox1 delete the file from the hard disk
save a new image to the hard disk and load it to the pictureBox1
delete the file and so on each time with a new image.
The problem for now is i'm getting exception on the line:
File.Delete(images[0]);
The process cannot access the file e:\screenshotsofpicturebox1\screenshot0.bmp because it is being used by another process.
Another problem I saw now it's saving each time a new file to the hard disk
screenshot0.bmp
screenshot1.bmp
But I wan to be only one file each time screenshot0.bmp and just to replace it each time with a new image.
Reading your code I assume you are trying to show the screen in the picturebox on each tick event.
So, if that is your objective, you don't need to save/delete it, just assign the Image object to the PictureBox Image property like this:
private void timer1_Tick(object sender, EventArgs e) {
Image refToDispose = pictureBox1.Image;
pictureBox1.Image = sc.CaptureWindowToMemory(windowHandle);
refToDispose.Dispose();
}
If you want to save/delete it anyway, you can't pass a directly loaded bitmap from the file to the PictureBox because it will lock the file while in use.
Instead you can create a graphics object from another Bitmap instance with the size of the image and draw it, so the new Bitmap will be a copy without the file lock that you can assign to the PictureBox.
In your code change this:
pictureBox1.Image = Image.FromFile(images[countimages]);
To this:
using (Image imgFromFile = Image.FromFile(images[countimages])) {
Bitmap bmp = new Bitmap(imgFromFile.Width, imgFromFile.Height);
using (Graphics g = Graphics.FromImage(bmp)) {
g.DrawImage(imgFromFile, new Point(0, 0));
}
pictureBox1.Image = bmp;
}
I found this code on here, and I want to save the image I get in my "pictureBox1" with a button like under, how can I implement these together?
I have the picture showing in the pictureBox1, I want to click a button and be able to store the picture on my PC.
private void button1_Click(object sender, EventArgs e)
This is the code to save image:
public static void SaveImageCapture(System.Drawing.Image image)
SaveFileDialog s = new SaveFileDialog();
s.FileName = "Image";// Default file name
s.DefaultExt = ".Jpg";// Default file extension
s.Filter = "Image (.jpg)|*.jpg"; // Filter files by extension
s.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
s.RestoreDirectory = true;
if (s.ShowDialog() == DialogResult.OK)
{
// Save Image
string filename = s.FileName;
(System.IO.FileStream fstream = new System.IO.FileStream(filename, System.IO.FileMode.Create))
{
image.Save(fstream, System.Drawing.Imaging.ImageFormat.Jpeg);
fstream.Close();
I am also not 100% sure i understand what you mean. but here is a 2 lines of codes, showing the simplest way of loading and saving an image into a picturebox.
// 1. Load a picture into the picturebox
PictureBox pic = new PictureBox() { Image = Image.FromFile("SomeFile.jpg") };
// 2. Save it to a file again
pic.Image.Save("SomeFilePath.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
I fixed it by doing this:
private void btnSave_Click(object sender, EventArgs e)
{
pictureBox1.Image.Save(#"C:\Temp\test.jpg",System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
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.
I have a project which is to read character in a captured image but I'm stuck at the button which is to scan image. I ended up tesseract dll in c#, but I don't know how can I code it. I'm a newbie to this programming.
private void Browse_Click(object sender, EventArgs e)
{
//FileInfo fi = new FileInfo(string.Format(#"C:\Documents and Settings\JOrce0201610\My Documents\Visual Studio 2005\Projects\OCR Reader\{0}", imageName));
OpenFileDialog fi = new OpenFileDialog();
fi.InitialDirectory = #"C:\\Documents and Settings\JOrce0201610\My Documents\Visual Studio 2005\Projects\OCR Reader\Card";
fi.Filter = "BMP Image|*.bmp";
fi.FilterIndex = 2;
fi.RestoreDirectory = true;
if (fi.ShowDialog() == DialogResult.OK)
{
//image file path
textBox1.Text = fi.FileName;
//display image in picture box
pictureBox1.Image = new Bitmap(fi.FileName);
}
}
private void Scan_Click(object sender, EventArgs e)
{
Bitmap temp = source.Clone() as Bitmap; //Clone image to keep original image
FiltersSequence seq = new FiltersSequence();
seq.Add(Grayscale.CommonAlgorithms.BT709); //First add GrayScaling filter
seq.Add(new OtsuThreshold()); //Then add binarization(thresholding) filter
temp = seq.Apply(source); // Apply filters on source image
If you are a 'newbie' to programming, OCR is not the best place to start. The best I can suggest is that you use a webservice or existing library that can do this for you.
Microsoft has project Hawaii, Hawaii has an OCR service which is quite easy to use.