C# - Drawing top image over background image (alpha channel) - WinForm - c#

I've got this topimage with alpha channel in it and I need to put this image over another background image, while the alpha channel from the top image stays intact obviously.
Now I've seen some tutorials with Canvas, but my project doesn't seem to recognize Canvas.
Anyone got an idea why I cant use Canvas or how to put those 2 images over each other?

Ok, I will try to answer: after loading the image, like this more or less, pseudocode:
Bitmap bmp = new Bitmap("MyCooolSemiTransparentImage.png");
bmp.MakeTransparent(colorHaveToBeRenderedTransparent);
colorHaveToBeRenderedTransparent is a color wich results non transparent after loading it into Bitmap object.
EDIT
if alphachannel is ok, here is a simple tutorial how to draw in image on WinForms:
msdn: DrawImage
Call method provided in yuor forms OnPaint override and you will get what you want.
Hope this helps.
Regards.

Related

Load 2 images in 1 picturebox - C#

How can I load 2 Images in one PictureBox ?
Here is an example:
http://postimg.org/image/l78kth897/
Thank you so much.
You can use Graphics.DrawImage() to draw any image anywhere inside a PictureBox or any other control for that matter. If you're writing your own control, override OnPaint(). If you want to use an existing PictureBox, simply use its Paint event to do this:
e.Graphics.DrawImage(YourImageObjectHere, ...);
e.Graphics.DrawImage(YourSecondImageObjectHere, ...);
GDI+ already supports transparency channel, so if your images have transparent areas, they'll draw just like the sample image you have posted. DrawImage() has over a dozen overloads, using which you can control several aspects of how an image is drawn. The simplest one takes the image object and the position to draw at.
Remember that an image object is an object of System.Drawing.Image or one of its derived classes. If all you have is the path of the image, you should use Image.FromFile() to create an Image object from that image file first.

How to WIC Encode a D2D bitmap to png

At the moment I'm programming a paint app in WPF C# and have a problem. I want to draw different shapes and save them as a png/bmp. So right now I'm decoding a bmp to a direct2d bitmap to draw on rendertarget. But my problem is that I don`t know how to save the D2DBitmap as a png because I can not find a encode function...
I am not allowed to use SharpDX so I hope someone can help me with this problem.
Or maybe somebody has another solution for drawing shapes and stuff but NOT in the xml file.
So I managed it by myself.
I created a surfaceRenderTarget that can be painted on and also is shown on Screen.
Then I created a WicBitmapRenderTarget with the same properties and this Rendertarget is easily saveable with a saveToFile method.
So I draw on the surfaceRenderTarget and push every operation to an operationstack and when I click the save button the programm draws the hole stack on the bitMapTarget and saves this.
This is my solution, maybe someone has a better.

WinRT masking image

I'm having a problem with masking an image in WinRT. Basically, what I need to do, is to cut out a puzzle shape out of the base image. I have the puzzle shape as a PNG black and white image, where the shape itself is white and the background black and also as a transparent shape of the puzzle piece. This is actually a port of a iOS app, where they used a CGContextClipToMask with the black and white mask to cut out the puzzle piece.
I tried using the Blit from the WriteableBitmapEx to mask the images, but I never achieved the result I wanted, the closest I got was the correctly cut out shape, but with a black background, instead of nothing. What is the correct way of cutting out this shape? Thanks for all the answers!
Indeed, WinRT/XAML in Windows 8 does not have an OpacityMask implementation of other XAML frameworks. You could use WriteableBitmap to manipulate the pixels, but it's a bit slow, especially on ARM devices. A faster solution is to use Direct2D, which has a FillOpacityMask method built right in. Since SharpDX wraps it nicely for .NET you can do that with C# too.
I don't have code, however the simplest case would be to just open PNG file in Photoshop/GIMP/any online transparency tool and just map black pixels alpha to zero.
Another example would be doing that in code directly,
WriteableBitmapEx has function to change each pixel,
all you have to do, is loop through all black pixels and change alpha to 0.

Draw a bitmap from a control taller than the screen

I am having quite a few problems saving a C# control to a bitmap file. The control in question is a kind of drawing surface on which the user can write text, draw pictures and paint boxes. The control is resizable and draggable. What happen is, when the control is really big and is not totally visible on the screen, the parts of the control not visible are saved half-drawn on the bitmap. The code used to generate the bitmap is quite simple:
Bitmap bitmap = new Bitmap(myControl.Width, myControl.Height);
myControl.DrawToBitmap(bitmap);
I have tried the following methods to try to have a fully painted bitmap, without any success:
myControl.Invalidate(myControl.ClientRectangle, true);
myControl.Refresh();
myControl.Update();
Application.DoEvents();
I cannot scale the control down to make it fully visible since resolution and image quality are very important for that project. In fact, I am actually trying to scale the image up to increase it's quality. Are there ways I am not aware of generating an image from a control ?
Tank you.
DrawToBitmap has limitations and dont always work as expected. Try instead work with native GDI+
Here is example
Maybe my answer here Capturing a Window that is hidden or minimized can help you?

C# Winform Image zoom with pixelate

I'm making simple image editor by C# winform.
I'm trouble with make zoom function. in other similar questions, many people simply suggest that 'change the size' such like..
Bitmap newImg = new Bitmap(oldImg, newWidth, newHeight);
But In this way, the picture become blured(is it caused by antialiasing? I don't know well...) I need pixelated zoom Image. Like any other image editor such as Photoshop or paint.net...
I tried also put pixelate function to make mosaic image. result was good but it was too slow!
please help me. How can I make pixelate zoom?
Check out Image resizing in .Net with Antialiasing, this should get you started (I'm not sure but setting SmoothingMode = SmoothingMode.None means no anitaliasing).

Categories

Resources