How to change backcolor of a image? - c#

How to change the BackColor of a transparent image to White using GDI objects without losing the content of the image.
When I tried to change it using the command
this.image.backcolor, the content of the image is lost.

create a bit map for image you want to change and use bitmap.clear(color u want);

First you create a new image with the desired backcolor and the same size as your original image. Then you draw the original image on the new image using Graphics.DrawImage.

Even if you want to use CSS trick to do this, Here is the solution:-
CSS
.reverse{-webkit-filter: invert(100%); filter: invert(100%);}
HTML
<img src="src path" class="reverse">
Same way many other options are available in CSS.
Hope this post will help you :).

Try this,
var newImage = new Bitmap(oldImage.Width, oldImage.Height);
using (var g = Graphics.FromImage(newImage))
{
g.Clear(Color.White);
g.DrawImage(oldImage, new Point(0, 0));
}

Related

C# Remove drawing from PictureBox

I have a PictureBox pic , and an Image img, pic.Image = img
and I draw some rectangles on the Image using
Graphics g = Grpahics.FromImage(pic.Image);
g.DrawRectangle(...);
But at one point, I want to remove the rectangle from the Image,I tried
pic.Image = getOriginalImage();
pic.Refresh();
but the image remains the same(Rectangles are still on top of the Image)
I know there's a method graphics.Clear(Color),but it replace the entire Image with a solid color
How do I remove the drawing only from the PictureBox?
Thanks
Edit:
I already saved the original and when I erase I use the original image
You are overwriting the original image. Save the image locally and clone it into the PictureBox.Image property. Then clone again when you want to erase
Draw the background image again using Graphics.DrawImage() or assign the image again to the Image property. Using a Graphics object in this way is writing directly to the image displayed in the PictureBox and it does not keep separate copies of what is written using a Graphics object and the background Image.

RichTextBox doesn't appear when content is exported as Image

I have a Panel where the contents are added dynamically and exported as an image file. I export the content as image using the following code below.
Bitmap tempBmp = new Bitmap(pnlCanvas.Width, pnlCanvas.Height);
pnlCanvas.DrawToBitmap(tempBmp, new Rectangle(0, 0, pnlCanvas.Width, pnlCanvas.Height));
tempBmp.Save(fileName);
In a particular case i have a RichTextBox control added to the panel. I found that the control is not seen when exported.
I am not sure what goes wrong. Please guide me what should be done.
Thanks in Advance,
K
As it is stated MSDN DrawToBitmap doesn't work with RichTextBox. Try painting the content using GDI+ manually.
Check if the control is actually there, is it disposed or just invisible. Try adding some value to it and check if it returns the value with variable or gives you error (if it's gone). That's my ideas :)
Just succeeded with exactly what i was looking for. I am answering my own question so that it could help someone looking for the same.
Sample code to capture the ActiveX controls and Export as Image.
Rectangle ctrlRect = myControl.RectangleToScreen(myControl.ClientRectangle);
Bitmap myImage = new Bitmap(ctrlRect.Width,ctrlRect.Height,PixelFormat.Format32bppArgb);
Graphics myGraphics = Graphics.FromImage(myImage);
myGraphics.CopyFromScreen(ctrlRect.Location, Point.Empty, myControl.Size);
myImage.Save("sample.png");

Save as a image all the content of a grid

So, I'm trying to save an canvas content as an image. I'm using this for it:
var img = new WriteableBitmap(myCanvas, null);
Problem is that the image is not showing all the content inside of the canvas. If there's a button, an image or other similar objects, they do not show.
I can see an ellipse, but if I set an image as background, the background stays empty.
Is there anyway to solve this?
Try to use Image Tools from codeplex and read this and this

C# how to get a bitmap from a picturebox

I have a image in picturebox. I want to get that image as a Bitmap.
My one line code is:
Bitmap default_image = (Bitmap)pictureBox5.Image.Clone();
But what i am getting is:
default_image value=null;
Can anyone help me.
Bitmap default_image = new Bitmap(pictureBox5.Image);
You are never instantiating a Bitmap which is why it is null.
If you got the image into the PictureBox by using imageLocation
pbSourceImage.ImageLocation = openFile.FileName;
then PictureBox.Image will be null.
Instead, load the picture using
pbSourceImage.Image = Image.FromFile(openFile.FileName);
Then you will be able to clone from the Image property.
This is because you do not have image, probably you have BackgroundImage.
You need to have Image properties fill with your picture.

How to render images in real-time

I am looking for a solution\software that I can use in order to render buttons in real time.
That is, I have an image and text, and I want them to be an image altogether in realtime.
Thank you
You can dynamically create an image based on text using the System.Drawing namespace.
private Bitmap CreateBitmapImage(string imageText, string imageUrl)
{
Bitmap myBitmap = new Bitmap(imageUrl);
Graphics g = Graphics.FromImage(myBitmap);
g.DrawString(imageText, new Font("Tahoma", 40), Brushes.White, new PointF(0, 0));
return (objBmpImage);
}
Once you have the Bitmap object in memory you can save it to the disk and call it's location from the web.
Bitmap bmp = CreateBitmapImage("my picture", #"C:\myBasePic.bmp");
bmp.Save(#"C:\bla.png", System.Drawing.Imaging.ImageFormat.png);
It would be good if you can specify why such requirement is there. One of such scenario that I had encountered was need of image buttons with different text (round corners with shaded background) - this can easily be achieved using CSS. If you really want to combine an image and text together then you can do that at server side (using say System.Drawing types) and serve the combined image over a handler/web-service.

Categories