Is there a way to re-size a transparent image in windows mobile(C#)? I can re-size the image but I am losing the transparency. It is being replaced with white.
Here is what I have now
public static void ResizePicture(string imageFileName, Size maxSize)
{
using (Image src = new Bitmap(imageFileName))
{
using (Bitmap dst = new Bitmap(maxSize.Width, maxSize.Height))
{
using (Graphics g = Graphics.FromImage(dst))
{
ImageAttributes imageAttr = new ImageAttributes();
g.Clear(Color.Transparent);
g.DrawImage(src, new Rectangle(0, 0, dst.Width, dst.Height), 0, 0, src.Width, src.Height, GraphicsUnit.Pixel, imageAttr);
}
dst.Save(imageFileName, ImageFormat.Png);
}
}
}
Have a look at this as found in the MSDN about setting the transparency color. It looks like you need to set the SetColorKey for this to work. See also here that explains you cannot set the transparency (apparently contradicting the first link). But, you can still down the Smart Devices Framework (community edition) found in OpenNetCF.org and use that instead to handle the transparency workaround. I would try the first link before going any further to see if that works.
Hope this helps,
Best regards,
Tom.
The CF has, in my opinion anyway, a bug. Graphics.Clear with Color.Transparent actually fills the image with white, not transparent. The workaround is tedious. See the link above.
Related
I want to know how to get and set pixel that are currently shown on the screen, I'm sure this must be possible I just don't know how. Would I need to use a DLL? I don't have any code to show at this point but I just needed to be pointed in the right direction, I can for the most part port from C# to F# if you can only answer in C#.
Look at this question:
How to read screen pixels for an application that is not in the foreground?
using (Bitmap bmpScreenCapture = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height))
{
using (Graphics g = Graphics.FromImage(bmpScreenCapture))
{
g.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, bmpScreenCapture.Size, CopyPixelOperation.SourceCopy);
}
Color c = bmpScreenCapture.GetPixel(x,y);
}
I am trying to precisely and predictably scale an image in C# to a different resolution, both up and down. When I open the resulting images with external tools such as Gimp, the results are not satisfying with my current settings.
public Image Square(Image image, int res) {
Bitmap sq = new Bitmap(res, res, image.PixelFormat);
Graphics canvas = Graphics.FromImage(sq);
canvas.CompositingQuality = CompositingQuality.HighQuality;
canvas.SmoothingMode = SmoothingMode.None;
canvas.InterpolationMode = InterpolationMode.Bicubic;
canvas.DrawImage(sq, 0, 0, res, res);
return sq;
}
The results are okay when scaling down (but far from perfect), but there are side-effects when scaling up:
This picture has a resolution of 2x2 pixels. The alpha channel is set to opaque for all pixels.
After scaling it to 4x4 pixels, this is the result:
Apparently, the C# graphics library introduced transparency while scaling the picture. This method should still work if the given image has transparent pictures, so removing the alpha channel is not an option.
Similarly, when scaling pictures down, there are problems at the edges of the resulting images as well, usually either very dark or transparent.
Is there any way to circumvent this behavior?
Edit: I already tried NearestNeighbor for downscaling only, but it results in this:
Edit 2: With WrapMode.TileFlipXY, the transparent edge is gone, but red only makes up 25% of the image instead of 50% as it should be:
You're asking for bicubic interpolation and you're getting it. What you want is the "nearest neighbor" option as outlined in the docs:
canvas.InterpolationMode = InterpolationMode.NearestNeighbor;
A way to avoid edge artifacts is to wrap the image:
using (ImageAttributes wrapMode = new ImageAttributes())
{
wrapMode.SetWrapMode(WrapMode.TileFlipXY);
g.DrawImage(input, rect, 0, 0, input.Width, input.Height, GraphicsUnit.Pixel, wrapMode);
}
Direct copy/paste from:
Ghost-borders ('ringing') when resizing in GDI+
I believe you need to combine NearestNeighbor interpolation with Half pixel offset. As pointed out in a similar question here.
canvas.InterpolationMode = InterpolationMode.NearestNeighbor;
canvas.PixelOffsetMode = PixelOffsetMode.Half;
I am making an windows form application in which i am overlaying an image during a webcam stream from the laptop.
the overlayed image is showing absurd colours. it is showing alot of pink colour. is there anything i can do to make the overlayed image look properly.
i am using the camera_Net Library to connect to the webcam
suggestions for overlaying an image during during a webcam video shall also be appreciated.
here is my code to draw the image
string filepath = #"E:\office\lux desktop app\Camera_Net-master\Camera_Net-master\Samples\CameraControlTool\water_PNG3290.png";
Bitmap bitmap1 = new Bitmap(filepath);
g.DrawImage(bitmap1, new Rectangle(400 , 0, 250, 600));
here is the look of the image during webcam stream
and here is the orignal image being overlayed
I think the problem is the image itself. It's an .png image, so I it has the possibility of an alpha-value. The white in the back is cropped out but in between the water, the light blue values are pink.
I would first try to use an easier image. Something like this. I think this will work just fine. Then search for some more complex images and try to find the weak-spot and find some alternative options to bring the image in to the cam.
As #Roman R pointed out that g point correctly to an incorrect background,
the problem was indeed with color format, so the solution is to use the correct image pixelformat based on your image.
here is the complete code
Bitmap bmp = new Bitmap(w, h, PixelFormat.Format32bppRgb);
Graphics g = Graphics.FromImage(bmp);
Image newImage;
newImage = Properties.Resources.red_frame_03;
using (Bitmap oldBmp = new Bitmap(newImage))
using (Bitmap newBmp = new Bitmap(oldBmp))
using (Bitmap targetBmp = newBmp.Clone(new Rectangle(0, 0, newBmp.Width, newBmp.Height), PixelFormat.Format16bppArgb1555))
{
g.DrawImage(targetBmp, new Rectangle(100, 0, 350, 350));
}
I want to make very simplistic paint/image editor. Mainly, for pixel editing, but that doesn't seem relevant.
To ease up my effort, I decided to keep the image size at 16x16.
I populate the form, add a PixelBox and slap a default image on it.
Of course, I need to make the pixels visible, set the interpolation to NearestNeighbor.
Then, I stretch the pixelbox to 320x320. And there the situation arises.
The image is displayed as thus:
Cropped image
Could someone shed some light on this? This is just a 16x16 image with a checkerboard pattern that I made, but I can't figure out why it is displayed with that offset at the top left.
Also, no code as been yet added. I assume this is default behavior?
If you look at the examples on the page that exact same error happens, so it must be a bug on the PixelBox.
Instead of using a custom control for this type of operation just use the standard PictureBox and scale the image by yourself:
public Bitmap ScaleBitmap(Bitmap src, Size NewSize)
{
Bitmap bmp = new Bitmap(NewSize.Width, NewSize.Height, src.PixelFormat);
Graphics g = Graphics.FromImage(src);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
g.DrawImage(src, new Rectangle(Point.Empty, NewSize), new Rectangle(0, 0, src.Width, src.Height), GraphicsUnit.Pixel);
g.Dispose();
return bmp;
}
I need to create an application in C# that captures part of the screen when certain part of the same screen changes. Thank you all.
You can use the System.Drawing.Graphics class. It has a CopyFromScreen method that will draw the content of a rectangular area of the screen into a Bitmap object.
It should do what you are after.
Have a look at this open source project called - Cropper. It is developed using C#.
Download the source code and have a look at it, you will get the basic idea of using System.Drawing classes.
Bitmap bmp = new Bitmap(rect.Width, rect.Height, PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(bmp);
g.CopyFromScreen(Left, Top, 0, 0, bmp.Size, CopyPixelOperation.SourceCopy);
bmp.Save(fileName, ImageFormat.Jpeg);