RichTextBox doesn't appear when content is exported as Image - c#

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");

Related

LiveCharts: How to convert chart to image using Windows Forms

I want to export LiveChart chart to image using WinForms. What I found till now is only WPF related stuff.
https://github.com/beto-rodriguez/Live-Charts/issues/243
Any help regarding WindowsForms will be much appreciated. Thanks
You can save your code using DrawtoBitmap method but to apply margin and padding
add your livechart control to panel and save panel using DrawtoBitmap method.
var chart = panel1;
using (var bmp = new Bitmap(chart.Width, chart.Height))
{
chart.DrawToBitmap(bmp, new Rectangle(0, 0, chart.Width, chart.Height));
bmp.Save("screenshotchart1.png");
}
Did u tried the Chart.SaveImage() method?
try the following code
this.chart1.SaveImage("C:\\mycode\\mychart.png", ChartImageFormat.Png);
As Documentation says, have you tried the RenderTargetBitMapClass?

Transparent images not showing properly [duplicate]

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.

How to change backcolor of a image?

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));
}

Adding image to rtf doc being resized

We're using migradoc api to create an rtf document.. intermittently when we add an image it;s being resized and coming out absolutely tiny.
Code sample as follows:
MigraDoc.DocumentObjectModel.Shapes.Image image = section.AddImage(imagePath);
image.WrapFormat.Style = MigraDoc.DocumentObjectModel.Shapes.WrapStyle.Through;
If I set LockAspectRatio to true and set a width it does stop is from rendering very small but ideally would like to be able to set a MaxWidth.
Has anyone experiences similar issue?
You have a choice of either changing the image itself by storing a different DPI value in it.
The .Net command to do so with a Bitmap bmp is:
bmp.SetResolution(newHRes , newVRes);
This should not involve reencoding, but I'm not sure.
However you also can simply set the desired DPI value in the Migradoc Image by using its Image.Resolution Property, which
Gets or sets a user defined resolution for the image in dots per inch.
I am not familiar with it at all but I found this code that might help you :
image.RelativeVertical = RelativeVertical.Page;
image.RelativeHorizontal = RelativeHorizontal.Page;

Display filter C#

It's a little hard to explain what I need but i'll try:
I need to write application (winform) which will be some kind of filter to image/other forms behind it. With one exception - all behind form should looks as is except of red (for example) color, which have to be replaced to any other specified color, white for example.
So let's imagine I have opened windows Word with few lines of text. With red and black letters.
So when i place my application above this text - it should "filter" red symbols and fill them to white.
So as i understand this task: i have to snap area behind the form, then process it (replace colors) and after draw this image on my form body.
Any links or keywords for solution?
UPD:
so - this is my final solution:
do form transparent (using TransparencyKey and BackColor properties)
place picturebox over the form
when we need to update image in picturebox - we replace current image with pictureBox1.Image = null;, then refreshing form with (this.Refresh()) and do new snapshot
thanks for all ;-)
UPD 2:
sample http://dl.dropbox.com/u/4486681/result.png
UPD 3:
here are sources
you can create a snapshot of the desktop using the following code:
public Bitmap CaptureScreen()
{
Bitmap b = new Bitmap(SystemInformation.VirtualScreen.Width, SystemInformation.VirtualScreen.Height);
Graphics g = Graphics.FromImage(b);
g.CopyFromScreen(0, 0, 0, 0, b.Size);
g.Dispose();
return b;
}
Replace the dimensions and position with the coordinates of your form. This way you get a bitmap of what's behind your form. Then you can do the color replacement on that bitmap.
Please note that due to settings like ClearType and other anti-aliasing mechanisms, you have to also take into account "intermediate pixels" when doing the color replacement. Otherwise things will look funny :-)
I don't know if this can be done at all (let's see what others answer :-).
You can get a handle to the screen device context, which gives you a bitmap of the screen.
HDC dc = GetDC(NULL);
(This is C++, you'll have to use P/Invoke, or create a mixed-mode library in C++)
Then you can redraw a region of the screen with your filtering process.
Now the problems start:
how do you know that the pixels in your interesting region has changed ?
if the region changes, are the changes visible or are they hidden by your own drawing.
You could have a button somewhere that hides your own app momentarily and shows it back when re-pressed, and filters the new content.
Good luck. Any possibility of sharing the user scenario ?

Categories