Rotate Image using RotateFlip in C# - c#

I have this code to rotate an image in an if loop in C# Windows Form application, but the Form does not show anything in the form output.
Can anyone help?
this.splitContainer1.Panel2.Controls.Add(PictureBox1);
PictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
PictureBox1.Image = bitmap; //Image.FromFile(#"C:\image.jpg");
PictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
PictureBox1.Image = (Image)(RotateImg(bitmap, 30.0f, Color.Transparent));

if you need to rotate an image on common angles you can use RotateFlip method with ease.
Please see my sample code:
string fileName = "somefile.png";
System.Drawing.Imaging.ImageFormat imageFormat = System.Drawing.Imaging.ImageFormat.Png;
Bitmap bitmap =(Bitmap)Bitmap.FromFile(fileName );
//this will rotate image to the left...
bitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
//lets save result back to file...
bitmap.Save(fileName, imageFormat);
bitmap.Dispose();
That's all, hope it helps.

Try this:
PictureBox1.Images.RotateFlip(RotateFlipType.Rotate180FlipX);
PictureBox1.Refresh();

Related

C# GDI+ ScaleTransform ok on picturebox but image saved is original

Hi I have the issue that when I use ScaleTransform(zoomFactor,zoomFactor) the image saved on disk is the original version always, while on screen in the picturebox the image is distorted in proportion to the zoomFactor.
Why this could be happening ? Shouldn't I have the final result as applied from e.Graphics on disk written image ?
My code is the following which is a version with matrix. but the instead of matrix I have used the ScaleTransform as well. Result is always the same:
g=e.Graphics;//inside picturebox_paint()
g.ScaleTransform(ratio * zoomFac, ratio * zoomFac);
e.Graphics.DrawImage((Bitmap)bmp, 0, 0);
int seed = Convert.ToInt32(Regex.Match(Guid.NewGuid().ToString(), #"\d+").Value);
String destinationFile = #"C:\tmp\photoid\" + new Random(seed).Next() + "_conv.jpg";
//Here I get always the original image back!!!!
bmp.Save(destinationFile);
I have used as well the following idiom but with same results:
//Matrix matrix = new Matrix();
//matrix.Scale(zoomFac, zoomFac);
//e.Graphics.Transform = matrix;
You need to make the PictureBox draw the things it shows on screen into a new Bitmap, which you then can save!
As it is the Image will be saved in the original form and nothing you did in the Paint event, which actually painst onto the surface of the PictureBox will be saved.
So to save everything, i.e. The Image, possibly a BackgroundImage and all you draw in the Paint event you would call DrawToBitmap somehwere.
Somewhere means somewhere else, not in the Paint event, as it will call the Paint event to create the new Bitmap, causing an endless loop..
To call it you would do something like this:
Bitmap bmpSave = new Bitmap(pictureBox1.ClientSize.Width, pictureBox1.ClientSize.Height);
pictureBox1.DrawToBitmap(bmpSave, pictureBox1.ClientRectangle);
But maybe this is not really what you want? Maybe you actually want to modify the Image? In that case do not use the Paint event at all!
Instead do something like this:
Bitmap bmpSave = new Bitmap(yourNewWidth, yourNewHeight);
using (Graphics g = Graphics.FromImage(bmpSave))
{
g.ScaleTransform(ratio * zoomFac, ratio * zoomFac);
g.DrawImage((Bitmap)pictureBox1.Image, 0, 0); //
pictureBox1.Image = bmpSave;
bmpSave.Save(...);
}
You could call this from somewhere where the scaling is being triggered from.
Note that doing the scaling repeatedly and each time from the previoulsy scaled version will degrade the quality rather fast. For this always scale from a saved version of the original!!
Btw: Using a Matrix for scaling doesn't really make a difference over ScaleTransform.
But if you want to do a direct scaling why not use the DrawImage overload which takes two Rectangles? This is the most common solution if all you want to to scale and maybe draw other stuff additionally..:
int newWidth = 100; int newHeight = 100; string yourFileName = "D:\\xyz123.jpg";
Bitmap bmpSave = new Bitmap(pictureBox1.ClientSize.Width, pictureBox1.ClientSize.Height);
Rectangle newRectangle = new Rectangle(0, 0, newWidth, newHeight);
Rectangle oldRectangle = new Rectangle(Point.Empty, pictureBox1.Image.Size);
using (Graphics g = Graphics.FromImage(bmpSave))
{
g.DrawImage((Bitmap)pictureBox1.Image, newRectangle, oldRectangle, GraphicsUnit.Pixel);
bmpSave.Save(yourFileName, ImageFormat.Jpeg);
}
And there there is the scaling Bitmap constructor:
Bitmap bmp = new Bitmap(pictureBox1.Image, newWidth, newHeight);
Which I would recommend if all you want is to scale the Image. As the other solutions it will not change the Image displayed until you assign it back into the PictureBox..:
pictureBox1.Image = bmp ;
Don't forget to dispose of the old Image..
Been a while since I messed with GDI but I think you need to copy back to the Bitmap here.
g.DrawImage(bmp, scaledwidth, scaledheight);
Try something like that before bmp.Save
Edit
Apologies for not seeing that you were copying back to the bitmap. Perhaps the overload which specifies the output rectangle is what you need. Try a DrawImage overload which has the destination Rect. https://msdn.microsoft.com/en-us/library/ms142040(v=vs.110).aspx

Draw 2 pictureboxes in one bitmap

I'm a c# beginner and I'm wondering if there is a way that I could draw 2 images from 2 different pictureboxes and save it as a one image using the method DrawToBitmap.
I can preview everything nice in the program itself (it looks good), but the main problem is when I save the picture, It's just showing the picturebox1 with a blank-alike icon of the picturebox2 in the middle :/
Here's the part of my code and it's not working as it should
pictureBox2.ImageLocation = potDoSlike;
Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
pictureBox1.DrawToBitmap(bmp, pictureBox1.Bounds);
pictureBox2.DrawToBitmap(bmp, pictureBox2.Bounds);
bmp.Save(#"D:\asd.jpg");
I figured it out! The problem was that I didn't save anything to the pictureBox.Image and it couldn't draw nothing out... well here's the code!
pictureBox2.Parent = pictureBox1;
pictureBox2.ImageLocation = potDoSlike;
pictureBox2.Image = Image.FromFile(potDoSlike);
Bitmap bmp = new Bitmap(pictureBox1.Image);
pictureBox1.DrawToBitmap(bmp, pictureBox1.Bounds);
pictureBox2.DrawToBitmap(bmp, pictureBox2.Bounds);
bmp.Save(#"D:\asd.jpg");

CopyFromScreen not working

This one is slightly confusing...
I am using Adobe's PDF Viewer control to view PDFs but I want the user to be able to drag an image onto the PDF and then when they click save it adds the image to the PDF at that location.
Implementing the PDF viewer proved quite difficult but I decided in the end to use Adobe's control, take a picture and then allow the user to draw the image ontop of the picture of the PDF. When they click save I am going to use PDFSharp to put the image onto the PDF once I've worked out where it goes but the problem I have at the moment is that I can't get a picture of the PDF.
The following code is used to get the picture but the Panel that it is attached to just appears with a white background with a red 'X' and border...
using (Bitmap bitmap = new Bitmap(adobePDFViewer1.Width, adobePDFViewer1.Height))
{
using (Graphics g = Graphics.FromImage(bitmap))
{
g.CopyFromScreen(new Point(adobePDFViewer1.Left, adobePDFViewer1.Top), Point.Empty, adobePDFViewer1.Size);
}
panelOverPdfViewer.BackgroundImage = bitmap;
}
I don't think this is the best way of doing it but I couldn't work out any others. Any help would be appreciated!
EDIT:
Following a very helpful answer below this is working code:
Here is the code I used:
Bitmap printscreen = new Bitmap(adobePDFViewer1.Width, adobePDFViewer1.Height);
Graphics graphics = Graphics.FromImage(printscreen as Image);
int left = this.Left + 396;
int top = this.Top + 30;
graphics.CopyFromScreen(left, top, 0, 0, printscreen.Size);
pictureBoxOverPDFView.Image = printscreen;
Look at this this Print-Screen
and try this for test work of CopyFromScreen
private void PrintScreen()
{
Bitmap printscreen = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
Graphics graphics = Graphics.FromImage(printscreen as Image);
graphics.CopyFromScreen(0, 0, 0, 0, printscreen.Size);
printscreen.Save(#"C:\Temp\printscreen.jpg", ImageFormat.Jpeg);
}

Display images with PictureBox in c#

I have write below codes in c# to display an image in PictureBox but when run the application, nothing shown...
Please help me to fix that.
here is my code:
private void button1_Click(object sender, EventArgs e)
{
PictureBox p =new PictureBox();
p.ImageLocation = "1.jpg"
p.Location = new Point(100, 75);
}
Add this line:
this.Controls.Add(p);
PictureBox.Image = new Bitmap("yourImage.jpg");
The formats supported
are: BMP, EMF, EXIF, GIF, ICON, JPEG, PNG, TIFF and WMF.
It's possible that the PictureBox size is small and your image is too big (check the SizeMode property to "StrechImage")
I was using a high res PNG icon with transparent background and took a little time to figure out.

Parameter is not valid exception when saving image in winform

I am getting a parameter is not valid exception..While saving the image
Here is my code to save the image
if (!File.Exists(pictureBox1.Tag.ToString()))
{
Image image = pictureBox1.Image;
image.Save(pictureBox1.Tag.ToString(), ImageFormat.Jpeg);
}
else
{
string delStr = pictureBox1.Tag.ToString();
pictureBox1.Image.Dispose();
File.Delete(delStr);
Image image = pictureBox1.Image;
image.Save(delStr, ImageFormat.Jpeg);
pictureBox1.Image = Image.FromFile(delStr);
}
In your else branch, you start by calling Dispose() on your Image, then you try to save it. That can't possibly work, because:
The Dispose method leaves the Image in
an unusable state.
Maybe you wanted to do:
string delStr = pictureBox1.Tag.ToString();
File.Delete(delStr);
Image image = pictureBox1.Image;
image.Save(delStr, ImageFormat.Jpeg);
image.Dispose();
pictureBox1.Image = Image.FromFile(delStr);
But that's still reloading the Image from the very file it has just been saved to, so it's not very useful. You might want to try something like:
string filename = pictureBox1.Tag.ToString();
if (File.Exists(filename) {
File.Delete(filename);
}
pictureBox1.Image.Save(filename, ImageFormat.Jpeg);
But then again, Image.Save() will overwrite the file anyway... So you only need to do:
pictureBox1.Image.Save(pictureBox1.Tag.ToString(), ImageFormat.Jpeg);
Am I seeing correctly? Are you disposing the image before saving it? If so here is your problem:
pictureBox1.Image.Dispose(); // THIS !!!!!! Just remove this line or move it to after save
Image image = pictureBox1.Image;
image.Save(delStr, ImageFormat.Jpeg);

Categories