This is the scenario, Suppose I have two pictureboxes. pbSrc displaying the Source Image and pbCrop displaying the cropped image. My code below shows the cropped of image from pbSrc to pbCrop. I tried saving it but the output was not as what I was expecting it to be :((((
pbCrop.Refresh();
//Prepare a new Bitmap on which the cropped image will be drawn
Bitmap sourceBitmap = new Bitmap(pbSrc.Image, pbSrc.Width, pbSrc.Height);
Graphics a = pbCrop.CreateGraphics();
//Draw the image on the Graphics object with the new dimesions
a.DrawImage(sourceBitmap, new Rectangle(0, 0, pbCrop.Width, pbCrop.Height), rectCropArea, GraphicsUnit.Pixel);
a.Save(); //Not sure if this really does saves the image into a file :v
Related
I am trying this example but I got "Source pixel format is not supported by the filter" error, and to solve it I tried with these answeres but I got titled error, then I tried to solve it with these answers.
But I am out of luck and I keep getting this error.
Can anyone give me a solution?
Heres the code:
// Open your image
string path = "sample2.jpg"; //taken from first example links initial image.
Bitmap image = (Bitmap)Bitmap.FromFile(path);
// The original bitmap with the wrong pixel format.
// You can check the pixel format with originalBmp.PixelFormat
//Bitmap originalBmp = new (Bitmap)Image.FromFile("YourFileName.gif");
// Create a blank bitmap with the same dimensions
Bitmap tempBitmap = new Bitmap(image.Width, image.Height);
// From this bitmap, the graphics can be obtained, because it has the right PixelFormat
using (Graphics g = Graphics.FromImage(tempBitmap))
{
// Draw the original bitmap onto the graphics of the new bitmap
g.DrawImage(image, 0, 0);
// Use g to do whatever you like
//g.DrawLine(...);
}
//Bitmap EditableImg = new Bitmap(image);
Bitmap a = AForge.Imaging.Image.Clone(tempBitmap, PixelFormat.Format8bppIndexed); //currently getting titled error here.
AForge.Imaging.Image.SetGrayscalePalette(a);
// create filter
DifferenceEdgeDetector filter = new DifferenceEdgeDetector();
// apply the filter
filter.ApplyInPlace(image);
error image for reference:
I need to crop sub-part from image.
For example,I have this image:
I need to crop the part of the image that in the red frame,
I have four coordinates of the frame corners,
Any idea how to implement it?
Thank you in advance.
You can use Graphics.DrawImage();
Rectangle cropRect = new Rectangle(...);
Bitmap src = Image.FromFile(fileName) as Bitmap;
Bitmap target = new Bitmap(cropRect.Width, cropRect.Height);
using(Graphics g = Graphics.FromImage(target))
{
g.DrawImage(src, new Rectangle(0, 0, target.Width, target.Height), cropRect, GraphicsUnit.Pixel);
}
And if need, you can save target to a new file.
Also See : C# Tutorial - Image Editing: Saving, Cropping, and Resizing
Does anyone know how to create a new bitmap from an existing image with a taller height, but don't scale the image and just have transparent, black or white below the original image in the new bitmap?
I basically have one picture that is taller than the second and I need the second one to be as tall as the first, without stretching it.
img2 = new Bitmap(lImages[2],new Size(pictureBox.Image.Width,pictureBox.Image.Height));
img2 = ((Bitmap)img2).Clone(new Rectangle(0, 0, pictureBox.Image.Width, pictureBox.Image.Height), System.Drawing.Imaging.PixelFormat.Format24bppRgb);
C# .NET 4.0.
By using a Graphics object, you can achieve this easily:
Bitmap temp = new Bitmap(new Size(pictureBox.Image.Width,pictureBox.Image.Height));
using(Graphics g = Graphics.FromImage(temp))
{
g.DrawImage(img2, 0, 0);
}
img2 = temp;
Now img2 references a new Bitmap object of the required size which has the original (unstretched) image painted on it.
Note: To control the color of the extra space, add a call to g.FillRect before drawing the image.
Create your "standart" size bitmap and fill it with, let's say, white color and call Bitmap.MakeTransparent(Color.White) and draw your final image over it.
I want to draw an image by reading it
from file sytem image file, and render
it on a document, below:
if (File.Exists(imageFilePath)) //all image file are 16*16 in pixel
{
Bitmap bitmap = new Bitmap(18, 18);
Graphics graphics = Graphics.FromImage(bitmap);
Image image = Image.FromFile(imageFilePath);
graphics.DrawImage(image, 1, 1); //1 pixel space in between
}
However, gray images renders the
correct size (18*18 in pixel),
whereas, colour images are cut off, or
have to increase the BitMap size (e.g.
Bitmap(23, 23). I want to all images
have the same size! The image sizes
are all the same on the file system.
It is a bit of urgency. Any idea would
be very much appreicated!
Solution below:
if (File.Exists(imageFilePath))
{
Bitmap bitmap = new Bitmap(18, 18);
Graphics graphics = Graphics.FromImage(bitmap);
Image image = Image.FromFile(imageFilePath);
graphics.DrawImage(image, 1, 1, **18, 18**);
}
I have a problem with the ListView control in a windows forms application.
Even if I create a thumbnail image or resize the real one I get distorted images in the list view.
The image looks like when you zoom in an image very much.
I first thought that the GetThumbnailImage is couseing this but I used a resize code I found here and I have the same result.
I also did not found any bug related to list view control so I gues I'm doing something wrong but I just can't figure out what.
Here is the code I use:
lsvPictures.LargeImageList = m_imagesList;
lsvPictures.LargeImageList.ImageSize = new Size(100, 100);
lsvPictures.View = View.LargeIcon;
lsvPictures.CheckBoxes = true;
for (int i = 0; i < ofd.FileNames.Length; i++)
{
filename = ofd.FileNames[i].ToString();
ListViewItem lvi = new ListViewItem(filename);
m_imagesList.Images.Add(ResizeImage(Image.FromFile(filename), 100, 100));
lvi.ImageIndex = i;
lsvPictures.Items.Add(lvi);
}
And this is the function that resizes images:
public static System.Drawing.Bitmap ResizeImage(System.Drawing.Image image,
int width, int height)
{
//a holder for the result
Bitmap result = new Bitmap(width, height);
//use a graphics object to draw the resized image into the bitmap
using (Graphics graphics = Graphics.FromImage(result))
{
//set the resize quality modes to high quality
graphics.CompositingQuality =
System.Drawing.Drawing2D.CompositingQuality.HighQuality;
graphics.InterpolationMode =
System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode =
System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//draw the image into the target bitmap
graphics.DrawImage(image, 0, 0, result.Width, result.Height);
}
//return the resulting bitmap
return result;
}
Thank you!
Mosu'
I just found the source of the problems:
m_imagesList.ColorDepth = ColorDepth.Depth16Bit;
It seams that, as default, the ColorDepth of the ImageList is 8 bit (or 4 bit, but my guess is 8). If I change this to at least 16 bit everything looks very nice.
To those with similar problems: I changed my Thumbnail method a lot before I realised that the ListView control is not using the color depth the images were having. I put the result of my method on a PictureBox control and saw that the function was working corectly. Atfer this I googled a lot ... and found that silly ColorDepth property.
How did you set the resolution for your image. Also, did what did you set the PixelFormat value to when you created the bitmap? I have a list of images loading into my list view that I am resizing similar to how you are and it is working fine without any distortion in the resulting thumbnail images that are created.
Here is a snippet from my resize method.
Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format24bppRgb);
bitmap.SetResolution(image.HorizontalResolution, image.VerticalResolution);
using (Graphics graphics = Graphics.FromImage(bitmap))
{
graphics.Clear(Color.Red);
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.DrawImage(image,
new Rectangle(destinationX, destinationY, destinationWidth, destinationHeight),
new Rectangle(sourceX, sourceY, originalWidth, originalHeight),
GraphicsUnit.Pixel);
}
return bitmap;
I was also using a ListView in WinForms to display directories, and had the same problem. I suggest that you check the image file type: icon files (.ico) tend to end up distorted, so try to use an image file with the .png extension. This works for me:
ListView listView = new ListView();
ImageList imageList = new ImageList();
// add image to list:
imageList.Images.Add("image_key", image_path);
// give the listview the imagelist:
listView.SmallImageList = imageList;
// add item to listview:
listView.Items.Add("item_text", "image_key");