Resize GIF and save animation? - c#

I'm using this code i wrote to resize my images to fit my PictureBox:
//Creates a new Bitmap as the size of the window
Bitmap bmp = new Bitmap(this.Width, this.Height);
//Creates a new graphics to handle the image that is coming from the stream
Graphics g = Graphics.FromImage((Image)bmp);
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
//Resizes the image from the stream to fit our windows
g.DrawImage(Properties.Resources.loading, 0, 0, this.Width, this.Height);
this.Image = (Image)bmp;
Works perfect !
The only problam is when im trying to resize a GIF... it resizes but i lose the animation...
Any fix for that?

You should simply set the PictureBox's SizeMode to StretchImage to make the PictureBox stretch the image for you.

Related

Cropping Image(From Screen)

I have read many question about this. But my question is little bit different. What i need to do crop image from screen.
There is my codes
Bitmap photo = new Bitmap(Screen.PrimaryScreen.Bounds.Width,Screen.primaryScreen.Bounds.Height)
Graphics gr = Graphics.FromImage(photo);
gr.CopyFromScreen(0,0,0,0 new size(foto.Width,foto.Height));
picturebox1.Image = photo;
And there my crop codes
Rectangle cropRec = new Rectangle(1,1,1,1);
Bitmap target = new Bitmap(cropRec.Width,cropRec.Height);
using(Graphics g = Graphics.FromImage(target))
{
g.DrawImage(photo,new Rentangle(0,0,target.Width,target.Height),cropRec,GraphicsUnit.Pixel);
}
I want to crop middle part of this photo and compare with itself.
Thanks n advance
To symmetrically crop your image as shown in the following sketch, try creating a Bitmap with margins in the X and Y axis and then cropping the screenshot image by using those margins in CopyFromScreen():
Bitmap printscreen = new Bitmap(Screen.PrimaryScreen.Bounds.Width - 2 * xmargin, Screen.PrimaryScreen.Bounds.Height - 2 * ymargin);
Graphics graphics = Graphics.FromImage(printscreen as Image);
graphics.CopyFromScreen(xmargin, ymargin, 0, 0, printscreen.Size);

Resize a PictureBox

I want to show a Resized Picture in my Picturebox.
The original picture is:
And the Picture in my form:
My picturebox size is 500x500px.
My method that I use for the resize:
public static Image ResizePicByWidth(Image sourceImage, double newWidth)
{
double sizeFactor = newWidth / sourceImage.Width;
double newHeigth = sizeFactor * sourceImage.Height;
Bitmap newImage = new Bitmap((int)newWidth, (int)newHeigth);
using (Graphics g = Graphics.FromImage(newImage))
{
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.DrawImage(sourceImage, new Rectangle(0, 0, (int)newWidth, (int)newHeigth));
}
return newImage;
}
I call the method with the original picture and the width from the picturebox.
But how can i resize the picture correctly?
I want my Form to show the whole Picture.
PictureBox has a SizeMode property. If you set this to Zoom it will automatically resize the image in it to fit inside it.

how do i merge multiple images into a single image ?

I have an array of Images all of the same size . I should add them to a new image like i have shown in the picture.
Different colors represent different images.
Identify the size of final image
Create a bitmap with final height and width var bitmap = new Bitmap(width, height);
Draw each image on canvas
using (var canvas = Graphics.FromImage(bitmap))
{
canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
//Draw each image (maybe use a loop to loop over images to draw)
canvas.DrawImage(someImage, new Rectangle(0, 0, width, height), new Rectangle(0, 0, Frame.Width, Frame.Height), GraphicsUnit.Pixel);
canvas.Save();
}
Save the final image bitmap.Save("image path", ImageFormat.Jpeg);

How do I resize an image without stretching it?

I am trying to resize an image (bitmap) in C# without stretching the image.
Say the image is 100x100 pixels.
I am looking to make it 100x110 pixels, and leave a white gap at the bottom of the image where it added the extra pixels.
I have done this, but cannot find a way to specify the pixel format. I need it to be 8bppindexed. I've attached an example to show the before and after image.
Here is the code I have so far.
string visit2 = "C:\\users\\moorez\\desktop\\visit2.bmp";
Bitmap orig = new Bitmap(visit2);
int width = orig.Width;
int height = orig.Height;
int newHeight = height + 2;
Bitmap newImage = orig.Clone(new Rectangle(0, 0, width, height), System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
newImage.Save("C:\\users\\moorez\\desktop\\visit3.bmp");
Bitmap test = new Bitmap(width, newHeight);
Graphics g = Graphics.FromImage(test);
g.DrawImage(newImage, new Point(0, 0));
test.Save("C:\\users\\moorez\\desktop\\visit4.bmp");
You can try this
Bitmap bmp = new Bitmap(newImage.Width, newHeight);
Graphics g = Graphics.FromImage(bmp);
g.Clear(Color.White);
g.DrawImageUnscaled(newImage, 0, 0, newImage.Width, newHeight);
bmp.Save(#"C:\\users\\moorez\\desktop\\visit3.bmp", ImageFormat.Jpeg);

Composition from multiple images using C#.NET

I have a PictureBox on which images from files are painted, one on top of another (like a photoshop layering concept, if you're familiar). Being PNGs and with a opacity index, these images are perfect candidates for a image composition. But I can't figure out how to do that and save to a file.
In the following code sample, I've loaded two PNG images into bitmap objects and had them painted on the PictureBox.
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Rectangle DesRec = new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height);
Bitmap bmp;
Rectangle SrcRec;
bmp = (Bitmap)Image.FromFile(Application.StartupPath + "\\Res\\base.png");
SrcRec = new Rectangle(0, 0, bmp.Width, bmp.Height);
e.Graphics.DrawImage(bmp, DesRec, SrcRec, GraphicsUnit.Pixel);
bmp = (Bitmap)Image.FromFile(Application.StartupPath + "\\Res\\layer1.png");
SrcRec = new Rectangle(0, 0, bmp.Width, bmp.Height);
e.Graphics.DrawImage(bmp, DesRec, SrcRec, GraphicsUnit.Pixel);
}
How do I save the composition to a file, preferably to another PNG file?
I would start drawing to an intermediate in-memory bitmap, which I would then save (and eventually draw in your picture box, if really needed). Something like this:
var bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
using (var graphics = Graphics.FromImage(bmp))
{
// ...
graphics.DrawImage(...);
// ...
}
bmp.Save("c:\\test.png", ImageFormat.Png);
Thanks both of you. I've decided to do as Efran Cobisi suggested and changed the program so that it does the composing in memory first. Then I can use it where ever and however I want.
My new code to reflect the changes is as follows-
// Image objects to act as layers (which will hold the images to be composed)
Image Layer0 = new Bitmap(Application.StartupPath + "\\Res\\base.png");
Image Layer1 = new Bitmap(Application.StartupPath + "\\Res\\layer1.png");
//Creating the Canvas to draw on (I'll be saving/using this)
Image Canvas = new Bitmap(222, 225);
//Frame to define the dimentions
Rectangle Frame = new Rectangle(0, 0, 222, 225);
//To do drawing and stuffs
Graphics Artist = Graphics.FromImage(Canvas);
//Draw the layers on Canvas
Artist.DrawImage(Layer0, Frame, Frame, GraphicsUnit.Pixel);
Artist.DrawImage(Layer1, Frame, Frame, GraphicsUnit.Pixel);
//Free up resources when required
Artist.dispose();
//Show the Canvas in a PictureBox
pictureBox1.Image = Canvas;
//Save the Canvas image
Canvas.Save("MYIMG.PNG", ImageFormat.Png);
Apparently, the images (Canvas) are being saved with opacity index intact.

Categories