C# PictureBox clean up and proper zoom - c#

I'm trying to clean the PictureBox in my Windows Form App before placing an image after it's been interpolated. I've searched for similar situations, but stuff like:
pictureBox1.Image=null;
pictureBox1.InitialImage=null;
pictureBox1.Invalidate();
pictureBox1.Dispose();
don't work in any combination. I used this method to open the image and put it on the PictureBox:
void openImage()
{
DialogResult dr = openFileDialog1.ShowDialog();
if (dr == DialogResult.OK)
{
file = System.Drawing.Image.FromFile(openFileDialog1.FileName);
pictureBox1.Image = file;
opened = true;
}
}
After that the image is interpolated with HighQualityBicubic, but the outcome is being placed on top of the original image. I used the Zoom property of PictureBox, so it won't crop the image or cover up the rest of the form.
Is there any way to remove the original picture and enlarge the outcome in the box?
Interpolation method (the guilty part of the code):
void bicubic(int newHeight,int newWidth)
{
Bitmap newImage = new Bitmap(file);
using (Graphics gr = Graphics.FromImage(newImage))
{
gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
gr.DrawImage(newImage, 0, 0, newWidth, newHeight);
}
pictureBox1.Image.Dispose();
pictureBox1.Image = null;
pictureBox1.Refresh();
//pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox1.Image = newImage;
}

You have to create a image with out come size. In your code you are creating the new image with original image size
Bitmap OrgImage = new Bitmap(file);
Bitmap newImage = new Bitmap(newWidth, newHeight);
using (Graphics gr = Graphics.FromImage(newImage))
{
gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
gr.DrawImage(OrgImage, 0, 0, newWidth, newHeight);
}
pictureBox1.Image = newImage;
this may solve your problem

PictureBox.SizeMode = PictureBoxSizeMode.StretchImage will stretch the image for you.
If you want other Sizing properties, just scroll trough the PictureBoxSizeMode enum.
As for reassignment, setting the PictureBox's Image property to null should be enough

Related

created image from code, looks pixelated when printing it

I am trying to print 40x40mm labels from a programmatically created image.
The label must have text on it, and a logo. Since the label is fairly small I am finding myself fiddling with how to do proper smooting, antialias and such.
I have tried multipl settings but I am not sure it's even the right way to go about it.
First I draw the container Bitmap:
private Bitmap DrawLabelCircle()
{
var labelImage = new Bitmap(152, 152);
using (Graphics gfx = Graphics.FromImage(labelImage))
{
var pen = new Pen(Color.Black, 1);
gfx.SmoothingMode = SmoothingMode.AntiAlias;
gfx.DrawEllipse(pen, 1, 1, 150, 150);
}
return labelImage;
}
Then I overlay different text snippets on that container Bitmap
private Bitmap DrawDistributorTextRectangle(string text)
{
var bitmap = new Bitmap(113, 113);
var rectangle = new Rectangle(0, 0, 110, 110);
using (Graphics gfx = Graphics.FromImage(bitmap))
{
gfx.SmoothingMode = SmoothingMode.AntiAlias;
gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
gfx.PixelOffsetMode = PixelOffsetMode.HighQuality;
var font = new Font(FontFamily.GenericSansSerif, 5, FontStyle.Regular, GraphicsUnit.Point);
var brush = new SolidBrush(Color.Black);
gfx.TextRenderingHint = TextRenderingHint.AntiAlias;
gfx.DrawString(text, font, brush, rectangle);
}
bitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
return bitmap;
}
Overlay that text on the previous created Bitmap.
private Bitmap DistributorTextOverlay(Bitmap source, Bitmap overlay)
{
var result = new Bitmap(source.Width, source.Height);
var graphics = Graphics.FromImage(result);
graphics.CompositingMode = CompositingMode.SourceOver;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.DrawImage(source, 0, 0);
graphics.DrawImage(overlay, 120, 0);
return result;
}
And the I save it.
var imageCodecInfo = ImageCodecInfo.GetImageEncoders().First(encoder => encoder.MimeType == "image/png");
var encoderInfo = new EncoderParameters() { Param = { [0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L) } };
image.SetResolution(203, 203);
image.Save("img.png", imageCodecInfo, encoderInfo);
The big challenge here is that the image I get is actually looking alright, all things considered.
But when I print it, it looks terrible pixelated.
I would really like to give some pointers for what settings I should apply to all these bitmaps before saving the final result, and what settings should apply for the final image I save.
I am by no means a .NET graphics expert so all help is much appreciated.
40mm is 1.5748 inches. So if you plan to print it at 300 dpi resolution, your bitmap should be 1.5748*300 = 472 pixels instead of 152.

How to save graphics created on a PictureBox?

In c# and Visual Studio Windows forms I have loaded an image into a picture box (pictureBox2) and then cropped it and show in another picture box (pictureBox3).
Now I want to save what is inside pictureBox3 as an image file.
How can I do this?
private void crop_bttn_Click(object sender, EventArgs e)
{
Image crop = GetCopyImage("grayScale.jpg");
pictureBox2.Image = crop;
Bitmap sourceBitmap = new Bitmap(pictureBox2.Image,
pictureBox2.Width, pictureBox2.Height);
Graphics g = pictureBox3.CreateGraphics();
g.DrawImage(sourceBitmap, new Rectangle(0, 0,
pictureBox3.Width, pictureBox3.Height), rectCropArea, GraphicsUnit.Pixel);
sourceBitmap.Dispose();
}
Never use control.CreateGraphics! Either draw into a Bitmap bmp using a Graphics g = Graphics.FromImage(bmp) or in the Paint event of a control, using the e.Graphics parameter..
Here is a cropping code that draws into a new Bitmap and that makes use of your controls etc but changes a few things:
It uses a Graphics object that is created from a new Bitmap
It make use of using clauses to make sure it won't leak
It takes the size of the pictureBox3.ClientSize so it won't include any borders..
private void crop_bttn_Click(object sender, EventArgs e)
{
Image crop = GetCopyImage("grayScale.jpg");
pictureBox2.Image = crop;
Bitmap targetBitmap = new Bitmap(pictureBox3.ClientSize.Width,
pictureBox3.ClientSize.Height);
using (Bitmap sourceBitmap = new Bitmap(pictureBox2.Image,
pictureBox2.ClientSize.Width, pictureBox2.ClientSize.Height))
{
using (Graphics g = Graphics.FromImage(targetBitmap))
{
g.DrawImage(sourceBitmap, new Rectangle(0, 0,
pictureBox3.ClientSize.Width, pictureBox3.ClientSize.Height),
rectCropArea, GraphicsUnit.Pixel);
}
}
if (pictureBox3.Image != null) pictureBox3.Image.Dispose();
pictureBox3.Image = targetBitmap;
targetBitmap.Save(somename, someFormat);
}
The alternative would be to..:
move all your code to the Paint event
replace the Graphics g = pictureBox3.CreateGraphics(); be Graphics g = e.Graphics;
insert these two lines to the click event:
Bitmap targetBitmap = new Bitmap(pictureBox3.ClientSize.Width,
pictureBox3.ClientSize.Height);
pictureBox3.DrawToBitmap(targetBitmap, pictureBox3.ClientRectangle);
The method PictureBox.CreateGraphics() should not be used unless you know what you are doing because it can cause some not-so-obvious problems. For example, in you scenario, the image in pictureBox3 will disappear when you minimize or resize the window.
A better way is to draw to a bitmap, which you also can save:
var croppedImage = new Bitmap(pictureBox3.Width, pictureBox3.Height);
var g = Graphics.FromImage(croppedImage);
g.DrawImage(crop, new Point(0, 0), rectCropArea, GraphicsUnit.Pixel);
g.Dispose();
//Now you can save the bitmap
croppedImage.Save(...);
pictureBox3.Image = croppedImage;
Btw, please use more reasonable variable names, especially for pictureBox1..3.

Why when cropping image i'm getting black image of the cropped image?

In form1 constructor
public Form1()
{
InitializeComponent();
BackColor = Color.LightGreen;
TransparencyKey = Color.LightGreen;
this.TopMost = true;
this.Location = new Point(0, 0);
timer1.Enabled = true;
}
Then:
private void Cap()
{
countImages++;
ScreenCapture sc = new ScreenCapture();
Image img = sc.CaptureScreen();
Bitmap bmp = new Bitmap(img);
Bitmap source = new Bitmap(this.Width, this.Height);
Rectangle section = new Rectangle(new Point(source.Width, source.Height), source.Size);
Bitmap CroppedImage = CropImage(source, section);
CroppedImage.Save("c:\\temp\\screens\\" + countImages + ".gif", ImageFormat.Gif);
source.Dispose();
}
And
public Bitmap CropImage(Bitmap source, Rectangle section)
{
// An empty bitmap which will hold the cropped image
Bitmap bmp = new Bitmap(section.Width, section.Height);
Graphics g = Graphics.FromImage(bmp);
// Draw the given area (section) of the source image
// at location 0,0 on the empty bitmap (bmp)
g.DrawImage(source, 0, 0, section, GraphicsUnit.Pixel);
return bmp;
}
The Bitmaps i save to the hard disk:
CroppedImage.Save("c:\\temp\\screens\\" + countImages + ".gif", ImageFormat.Gif);
They are all black.
I want to crop this part from the image and save it to the hard disk.
Example of screenshot and what i want to crop from it:
I marked with a red circle the transparent form1 and this is what i want to crop the part of the form1 and save this as cropped image the form1 part:
I tried this first:
private void CaptureDesktop()
{
countImages++;
ScreenCapture sc = new ScreenCapture();
Image img = sc.CaptureScreen();
Bitmap bmp = new Bitmap(img);
Bitmap source = new Bitmap(this.Width, this.Height);
Rectangle section = new Rectangle(new Point(source.Width, source.Height), source.Size);
Bitmap CroppedImage = CropImage(bmp, section);
CroppedImage.Save("c:\\temp\\desktopscreens\\" + countImages + ".gif", ImageFormat.Gif);
}
But i'm getting this cropped image as result: Strange picture in picture ?
Then i tried this code:
using (Bitmap bmpScreenCapture = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height))
{
using (Graphics g = Graphics.FromImage(bmpScreenCapture))
{
g.CopyFromScreen(this.Location.X,
this.Location.Y,
0, 0,
this.Size,
CopyPixelOperation.SourceCopy);
}
Rectangle section = new Rectangle(new Point(this.Location.X, this.Location.Y), new Size(this.Width, this.Height));
Bitmap CroppedImage = CropImage(bmpScreenCapture, section);
CroppedImage.Save("c:\\temp\\desktopscreens\\1.bmp", ImageFormat.Bmp);
}
But the result is:
Not what i wanted. I want only to crop the form1 part from the whole image.
You do Bitmap source = new Bitmap(this.Width, this.Height); this creates a black image.
Then you proceed to crop it. That's still a black image.
Then you proceed to save it. That's still a black image.
Perhaps you meant to crop Bitmap bmp or Image img, not Bitmap source. I don't know what Bitmap source is meant to be.
Try this (some code from Capture the screen shot using .NET)
private void Cap()
{
using (Bitmap bmpScreenCapture = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height))
{
using (Graphics g = Graphics.FromImage(bmpScreenCapture))
{
g.CopyFromScreen(this.Location.X,
this.Location.Y,
0, 0,
this.Size,
CopyPixelOperation.SourceCopy);
}
Rectangle section = new Rectangle(new Point(this.Location.X, this.Location.Y), new Size(this.Width, this.Height));
Bitmap CroppedImage = CropImage(bmpScreenCapture, section);
CroppedImage.Save("c:\\temp\\screens\1.bmp", ImageFormat.Bmp);
}
}
public Bitmap CropImage(Bitmap source, Rectangle section)
{
// An empty bitmap which will hold the cropped image
Bitmap bmp = new Bitmap(section.Width, section.Height);
Graphics g = Graphics.FromImage(bmp);
// Draw the given area (section) of the source image
// at location 0,0 on the empty bitmap (bmp)
g.DrawImage(source, 0, 0, section, GraphicsUnit.Pixel);
return bmp;
}

Why does my image resize code always product a completely black output image?

I'm trying to resize an image. I thought it was a simple task...
Here's my code (note, the two Save calls are just for debugging to illustrate the problem):
var newSize = new Size { Width = 450, Height = 250 };
using (var img = (Bitmap)Image.FromFile(sourceImageFilename))
{
var outputImage = new Bitmap(newSize.Width, newSize.Height);
// Save input image for debugging (screenshot below)
img.Save(#"M:\Coding\Photos\Temp\input.jpg");
using (Graphics gr = Graphics.FromImage(img))
{
gr.SmoothingMode = SmoothingMode.HighQuality;
gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
gr.DrawImage(outputImage, new Rectangle(0, 0, newSize.Width, newSize.Height));
}
// Save output image for debugging (screenshot below)
outputImage.Save(#"M:\Coding\Photos\Temp\output.jpg");
}
This appears to be the exact same code a ton of people are using (and exists on SO in many answers). However, here's what the two images that are being written to disk look like:
The original image is 5344x3006 and newSize (and the black output image) are 450x250.
All my other code is working fine (reading pixels from the input image with SetPixel, etc.), it's just this resize that's broken. Doing the resize with the Bitmap constructor is fine (but a bad quality resize).
You need to get the graphics from the OutputImage.
public static Bitmap Scale(this Bitmap inputImage, Size newSize)
{
var outputImage = new Bitmap(newSize.Width, newSize.Height);
inputImage.Save(#"M:\Coding\Photos\Temp\input.jpg");
using (Graphics gr = Graphics.FromImage(outputImage))
{
gr.SmoothingMode = SmoothingMode.HighQuality;
gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
gr.DrawImage(inputImage, new Rectangle(0, 0, newSize.Width, newSize.Height));
}
outputImage.Save(#"M:\Coding\Photos\Temp\output.jpg");
return outputImage;
}

Graphics object making blank images

I'm trying to draw images to return as base 64 strings over a web service in .net 4.5. I can get as far as loading a custom background, but then I need to draw text onto that background. The problem is, once I go from Image->Graphics object->Image, I end up with a blank png. If I return the original srcImage (the blank template) over the service, everything works, but my label is blank so I know it must be a problem with my graphics object somewhere.
My code is:
var labelSize = new Size(400, 459);
using (var srcImage = Image.FromFile(HostingEnvironment.MapPath("~/images/labels/" + labelImageFilename))) {
PixelFormat format = srcImage.PixelFormat;
using (Bitmap newImage = new Bitmap(labelSize.Width, labelSize.Height, format))
using (Graphics g = Graphics.FromImage(newImage)) {
g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
Rectangle srcRect = new Rectangle(0, 0, srcImage.Width, srcImage.Height);
Rectangle destRect = new Rectangle(0,0, labelSize.Width, labelSize.Height);
g.DrawImage(srcImage, destRect, srcRect, GraphicsUnit.Pixel);
// draw other shapes etc
g.FillRegion(Brushes.Blue,new Region(new Rectangle(0,0,200,200)));
g.Clear(Color.Red);
g.Flush();
return new Bitmap(srcImage, labelSize.Width, labelSize.Height); // this works fine, but my image is just the standard background I'm using
return new Bitmap(labelSize.Width, labelSize.Height, g); // returns a blank image
}
}
Nothing is drawn, neither the template background (srcImage), not the red or blue rectangles.
I think it looks like your graphics object is drawing on newImage, but you are returning srcImage. If you want to return the result of your drawing, I think you need to return newImage.
Try something like:
return new Bitmap(newImage, labelSize.Width, labelSize.Height);

Categories

Resources