Put .Gif Image from Clipboard to PictureBox - c#

I want to copy gif image from Browser and paste to PictureBox
Image cImage = Clipboard.GetImage();
pictureBox1.Image = (Image)cImage;
This put image, but its not animated.

The image on the clipboard isn't a GIF, it's a Bitmap or DIB (or both). Those are the formats that you would see, if you used the old XP Clipboard Viewer (clipbrd.exe), or if you enumerated the available clipboard formats within your program.
See the list of standard clipboard formats: http://msdn.microsoft.com/en-us/library/windows/desktop/ff729168(v=vs.85).aspx
GIF is not one of them.
Also, there is no JPG or PNG. When you copy those images, they're placed onto the clipboard as CF_BITMAP and/or CF_DIB. Basically, Bitmap is the universal image format that everything converts to/from.
As an alternative, you MAY be able to get CF_HTML from the clipboard, figure out where the image is, and then fetch it from the server (or maybe the browser cache), preserving the original GIF information.

Related

Save original image from PictureBox, not resized

Is it possible to save original image with original quality from PictureBox, not the stretched/resized one? I download byte array from a server and put it in PictureBox, I want user to be able to save the actual image, but it should be the original image (original quality and original size) I received from the HTTP server, not resized and reduced quality one that I show in a PictureBox. Is it possible or do I have to store the byte array I receive from server somewhere in order to achieve this?
Image property of the PictureBox contains the original image, while the control may paint the image as zoom/resized/stretched depending to its SizeMode. You can see the source code for the property here.
Just save the Image property and it will be the same image which you had read from database.
So calling pictureBox1.Image.Save will be enough.

How I can show text over an image and convert this to PNG or JPG in runtime with unity3d?

I'm try to generate a text over a image like the game "dulp". I need to know how to convert this to PNG or JPG with the text that is generated according to the player score in runtime to save the picture.
I don't need take a screenshot, I need put a modifiable text over the texture to convert in PNG or JPG.
See the example image, Image generated of the game "dulp"

Re-encoding of Jpeg Image in binary column when saving

I know there are similar questions, but I have a question concerning the storage of images in a binary-column.
I have a small windows forms app that loads an image into a picturebox control from a sql compact db using Linq2SQL. The user can drag any image (jpg,bmp,gif) on a picturebox. On the DragDrop-Event the image is loaded into the picturebox.
When I save the record following code is executed to store the image of the picturebox control:
MemoryStream imgStream = new MemoryStream();
pictureBox1.Image.Save(imgStream, System.Drawing.Imaging.ImageFormat.Jpeg);
myTable.MyImage = imgStream.ToArray();
I have checked the size of the byte array and it didn't change after saving the record.
Is the image re-encoded every time the Save-Method is called? It would be maybe then better to check if the image has changed at all.
JPEG images are decoded as a function of their being displayed by .NET (or really just about anything). So, if you pull a JPEG out as a binary, put it in a PictureBox (which will convert it to a raster format to display), then take that now-uncompressed raster Image and recompress it, you MAY end up making changes to that image.
I would keep the original bytestream for the displayed Image somewhere behind the scenes, and write that back to the DB when the user saves the data. This will not only help preserve the integrity of the image, it will boost performance by reducing the need to recompress the image every time.
If this code is called when you call Save then yes, the PictureBox will "export" itself as a JPEG every time. Are you noticing a performance issue because of this? If you want to avoid it, set a flag on application load and when the drag/drop event occurs raise the flag signaling that the Save method should update the image data.

32bit rgb bmp to 24bit rgb bmp in C# problem

I have to convert 32bit rgb bmp images in to 24 bits rgb bmp.
This is what i am trying to do
Bitmap b1=new Bitmap(sorecFileName);
Bitmap b2=new
Bitmap(b1.Size.Width,b1.Size.Height,System.Drawing.Imaging.PixelFormat.Format24bppRgb);
b2.SetResolution(b1.HorizontalResolution, b1.VerticalResolution);
Graphics g=Graphics.FromImage(b2);
g.DrawImage(b1,0,0);
//continue to draw on g here to add text or graphics.
g.Dispose();
b2.Save(destinationFileName);
The code compiles fine and generates the output image of 24bpp but its not in the rgb format any more. Why is this so?
I figured it out as I have a library that takes input of an image as rgb24 and displays it. So when I try to give the file generated by above code as input to the function, it displays noisy image.
However, if I open the same file in paint and save it as 24bpp bmp, and input it to the function, the picture displays fine. What am I missing?
b2.Save(destinationFileName);
You didn't specify the image file format. The default is PNG, not BMP. You now probably have a .bmp file on disk that actually contains a PNG image. That can go undetected for quite a while, lots of graphics programs pay attention to the file header instead of the file name extension. MSPaint for example will have no trouble loading the file. Just like any other program that uses GDI+. You might not be so lucky with a program that blindly assumes that the file contains a BMP and does no checking at all. Fix:
b2.Save(destinationFilename, System.Drawing.Imaging.ImageFormat.Bmp);
if you got a blackimage -
Add g.Clear(Color.White);

Screen Capture Feature Overrides

what i want to do is write an application in C# like "fraps" and other scren capture apps that when you press the "Print Screen" it saves the current screen as an image.
What i thought of is that
"i could create a background worker thread that checks the clipboard after x seconds and if there is an image, that is now in clipboard because of print screen click, it writes the clipboard contents to a jpeg or bitmap file" bu i lack the following knowledge
How will i know that there is an image in clipboard or some text or file
how to write that clipboard to an image file like how to convert that data into a JPEG (LZ-W format) or bitmap format etc.
Can anybody endow me with some knowledge or guide or help from C# coding prespective
The saving to a particular format is incredibly easy thanks to the Image class:
myImage.Save("someimage.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
As far as checking the clipboard, well you can do this, but i think you might run into issues that you won't know whether the image came from a Print-Screen, or from a Ctrl-c that the user did. However you can easily check the clipboard:
if (Clipboard.ContainsImage())
myImage = Clipboard.GetImage();

Categories