LiveCharts: How to convert chart to image using Windows Forms - c#

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?

Related

Lumia imaging crop filter

Hi so I'm making an app for Windows 10 that requires a user to choose an image and it's going to crop the image to 310*128. I got the file picker code already. But I want to know how to actually crop and save the image and display in image box. I already have the xaml page done
With Lumia Imaging SDK you can both crop the selected image and resize the image. In this answer I assume you actually want to crop, but from the text I could just as well guess you really just want to resize.
For crop, use the CropEffect from Lumia.Imaging.Transforms. Set the CropArea property on it to the object, and then render it. If you are rendering straight to the XAML page I recommend using a SwapChainPanel object in XAML and a SwapChainPanelRenderer to render on it.
Given that you are loading a StorageFile and rendering to a SwapChainPanel your code might look like something like this:
StorageFile file = ...
using (var source = new StorageFileImageSource(file))
using (var crop = new CropEffect(source, new Rect(0, 0, 310, 128))
using (var renderer = new SwapChainPanelRenderer(crop, YourSwapChainPanel))
{
await renderer.RenderAsync();
}

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 a logo image to a chart using Microsoft Chart Control

I have a chart that is built using Microsoft Chart Controls and would like to add our company's logo to the corner. Is this possible to do with MCC?
Is there any reason you don't want to simply put a PictureBox control near the corner of the chart? That seems like that simplest solution to me.
To add an image, use ImageAnnotation.
ImageAnnotation logo = new ImageAnnotation();
logo.X = 75;
logo.Y = 60;
logo.Image = "imagePath";
chart.Annotations.Add(logo);

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

Taking screenshot of ASPX page

is there an easy way (preferably without having to import libraries) to take a screenshot of an ASP.NET web page (better yet an aspx control) in c# and saving it as an image? Many thanks in advance! Sample code or a link to a tutorial would be greatly appreciated...
One really kludgie solution: Write a WinForms app and include a Browser control. Navigate to the web app page you're trying to capture, and then use the programmatic screen capture approach described here.
Here is a simple screenshot maker, wrote a few years ago. I am not sure what you like to achieve, but this one takes a screenshot of the whole screen. Hope this helps.
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;
public class ScreenshotManager
{
private Image screenshot;
public Image Screenshot
{
get
{
if (screenshot == null)
MakeScreenshot();
return screenshot;
}
}
public MemoryStream ScreenshotToMemoryStream()
{
MemoryStream ms = new MemoryStream();
Screenshot.Save(ms, ImageFormat.Jpeg);
ms.Position = 0;
return ms;
}
public byte[] ScreenshotToByteArray()
{
return ScreenshotToMemoryStream().ToArray();
}
public void MakeScreenshot()
{
screenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
var graphics = Graphics.FromImage(screenshot);
graphics.CopyFromScreen(0, 0, Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
}
}
Not 100% sure if this is exactly what you are looking for, but this tutorial should at least give you the ground work ability to capture a screen shot and save it. This more appears to be capping the entire screen, rather than just the aspx page, but at least it should be a start.
http://www.geekpedia.com/tutorial181_Capturing-screenshots-using-Csharp.html
http://weblogs.asp.net/jalpeshpvadgama/archive/2008/01/28/how-to-take-screenshot-in-c.aspx
The biggest piece of that seems to be
using System.Drawing.Imaging;
Should be able to hash things out from there, I believe
You can programmatically create a web browser control and take a screenshot of its client area -contrary to popular belief the web browser does not have to be visible for this - just make sure the thread you use the WebBrowser control on runs in apartment state ApartmentState.STA. This approach will work both on the server and client side.
On a high level what you have to do to create the bitmap:
Instantiate the WebBrowser control
and set the desired width/height
Browse to the URI of your choice
Wait for the DocumentCompleted
event
Use the WebBrowser DrawToBitmap()
method to extract the image

Categories