I'd like to understand how to use C# for drawing an image in my browser window - alongside other HTML objects that are already there. (Idea: To place buttons and forms that allow the user to alter the image.)
I am doing this in Mono on Linux, but I'd like the thing to be portable.
What I came up with so far is:
public virtual void button1Clicked (object sender, EventArgs args)
{
int height = 300;
int width = 300;
Bitmap bmp = new Bitmap(width, height, PixelFormat.Format24bppRgb);
Graphics g = Graphics.FromImage(bmp);
//
//Draw something on g here
//
Response.ContentType = "image/gif";
bmp.Save(Response.OutputStream, ImageFormat.Gif);
g.Dispose();
bmp.Dispose;
}
This clears the browser window and then displays the image, which is not what I wanted.
After extensive search I found the following tutorial,
http://www.dreamincode.net/forums/topic/67275-the-wonders-of-systemdrawinggraphics/
which says I should try to use a Handle on some control in my window, alternatively the CreateGraphics() method. Neither of these seem to exist for my "button1" or any other id of an entity in my .aspx page. Is that because I'm using Mono?
To sum up, what is the best way to draw inside a browser window in C# + asp.net??
i had the same thing to do some time ago,
the thing is that you will have to save your graphic object/bitmap to a location if you want to use it in an iFrame or Image control.
If you don't want to save it to a location,
try using a server script on your page :
http://www.developerfusion.com/article/2569/creating-images-on-the-fly-with-aspnet/
Related
I have managed to implement a video playing capability to my WinForms Application Project using DirectX's Video Class (although old and deprecated). My goal was to use this video as a background for a specific form with minimal controls. I've tried using GIF files but the color quality is just not good enough, using APNG on the other hand leaves me with a hilariously large .apng file size.
As Dx's Video Class needs a control to play the video into - i've used a PictureBox Control to make do where a Panel Control can also substitute for this. My Next Goal is to draw/render a text over the PictureBox Control which acts as the host for the Video. This is due to that Label Controls are not transparent nor does it support transparency in 100% so i am trying to improvise by rendering a string using Graphics.DrawString() or TextRenderer.DrawText().
I've tried both but neither was able to display the text as i've expected it to be.
Below is my code snippet over the Paint Event for the PictureBox Control:
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
var font = new Font("Helvetica Neue LT Std 65", 15);
var brush = new SolidBrush(Color.White);
var point = new Point(55, 150);
string welcomeText = "Welcome, User!";
// e.Graphics.DrawString(welcomeText, font, brush, point);
TextRenderer.DrawText(e.Graphics, welcomeText, font, point, Color.White);
pictureBox1.Refresh();
}
when running my source code, nothing happens actually, just like in this GIF: https://imgur.com/3uYdCF1
however, what i'm expecting is something like this:
Is it even possible to achieve this in the first place with WinForms? If so, i'd like some help to implement this. Thank you.
I have a fun application written in Visual Studio (C#), and I'm wondering - What I do is I draw panels in certain ways on the form - if it's possible to render the form, without taking a screenshot?
Thanks
If by "taking a screenshot" you mean "sending PrtSc", then there's a better way, using System.Drawing.Graphics.CopyFromScreen:
using(Bitmap b = new Bitmap(this.ClientSize.Width, this.ClientSize.Height)) {
using(Graphics g = Graphics.FromImage(b)) {
g.CopyFromScreen(this.PointToClient(Point.Empty), Point.Empty, this.ClientSize);
}
// Your form is now rendered into b.
}
If you want to include the border, just use Size instead of ClientSize, and this.Location instead of this.PointToClient(Point.Empty).
Alternatively, you can use this.DrawToBitmap:
using(Bitmap b = new Bitmap(this.Width, this.Height)) {
this.DrawToBitmap(b, new Rectangle(0, 0, this.Width, this.Height));
// Your form is now rendered into b.
}
This will work even if your form doesn't have focus. However, it will draw the border and it will draw it in Windows Basic style if Aero is active.
No, it's not possible to capture the visual of the form as an image other than what is offered by some sort of screenshotting library (if that's what you're asking).
I have a form that has 2 panels. I'm trying to save the contents of Panel2 as an image. I saw a thread that talked about using the screen capture to do this, but I can't find the thread anymore. Also read about using the DrawToBitMap method, but it's from visual studio 2005 info, not sure if it's the most current or suitable solution for this. So what do you recommend for saving my Panel2 as a picture, preferably a jpg?
UPDATE:
I implemented the code recommended below for the DrawToBitMap, but it saves half of my panel2 (the left half if that makes a difference).
Because it saved half my panel2, I multiplied the width call by '2' to make it save the full form. Kind of a weird thing and doesn't make sense to me since the width of panel 2 should be the full panel and not half of it?
//multiplies the width of panel2 call by 2 to make it save the full panel
Bitmap bmp = new Bitmap(splitContainer1.Panel2.Width * 2, splitContainer1.Panel2.Height);
splitContainer1.Panel2.DrawToBitmap(bmp, splitContainer1.Panel2.Bounds);
bmp.Save(#"C:\Test.bmp");
Control.DrawToBitMap is still supported in .Net 4. With the following caveats.
From above link:
The DrawToBitmap method is not supported for ActiveX controls. You
can override the OnPrint event and provide custom printing logic if
required.
The DrawToBitmap method has the following limitations:
An ArgumentException might be thrown for large bitmaps. The maximum
allowable size varies by machine.
DrawToBitmap does not support the Ink controls for the Windows XP
Tablet PC Edition 2005 operating system.
DrawToBitmap does not draw a child TextBox if the Visible property of
the TextBox is set to false.
Controls inside containers are rendered in reverse order.
DrawToBitmap is not fully functional for the RichTextBox; only the
border of a bitmap is drawn.
Edit Added example and image:
Bitmap bmp = new Bitmap(panel1.Width,panel1.Height);
panel1.DrawToBitmap(bmp, panel1.Bounds);
bmp.Save(#"C:\Temp\Test.bmp");
namespace PanelToPDF
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnExit_Click(object sender, EventArgs e)
{
pictureBox.Dispose();
Application.Exit();
}
private static Bitmap DrawControlToBitmap(Control control)
{
Bitmap bitmap = new Bitmap(control.Width, control.Height);
Graphics graphics = Graphics.FromImage(bitmap);
Rectangle rect = control.RectangleToScreen(control.ClientRectangle);
graphics.CopyFromScreen(rect.Location, Point.Empty, control.Size);
return bitmap;
}
private void btnToImage_Click(object sender, EventArgs e)
{
Bitmap bitmap = DrawControlToBitmap(panel);
pictureBox.Image = bitmap;
}
}
}
I try to take a screenshot of an application writen in WPF and the application is not captured, must I use a special tool to take the screenshot?
You can use RenderTargetBitmap to generate an image from your WPF control.
public const int IMAGE_DPI = 96;
public Image GenerateImage(T control)
where T : Control, new()
{
Size size = RetrieveDesiredSize(control);
Rect rect = new Rect(0, 0, size.Width, size.Height);
RenderTargetBitmap rtb = new RenderTargetBitmap((int)size.Width, (int)size.Height, IMAGE_DPI, IMAGE_DPI, PixelFormats.Pbgra32);
control.Arrange(rect); //Let the control arrange itself inside your Rectangle
rtb.Render(control); //Render the control on the RenderTargetBitmap
//Now encode and convert to a gdi+ Image object
PngBitmapEncoder png = new PngBitmapEncoder();
png.Frames.Add(BitmapFrame.Create(rtb));
using (MemoryStream stream = new MemoryStream())
{
png.Save(stream);
return Image.FromStream(stream);
}
}
private Size RetrieveDesiredSize(T control)
{
if (Equals(control.Width, double.NaN) || Equals(control.Height, double.NaN))
{
//Make sure the control has measured first:
control.Measure(new Size(double.MaxValue, double.MaxValue));
return control.DesiredSize;
}
return new Size(control.Width, control.Height);
}
Note that this will generate a PNG image ;) If you wish to store it as a JPEG, I suggest you use another encoder :)
Image image = GenerateImage(gridControl);
image.Save("mygrid.png");
You can simply press PrtScr button (windows will copy whole descktop image to buffer), then paste it in Power Point and crop if you like.
I'm having the same issue, I need to take screenshots to document my tests but can't seem to get there.
The Window in question is a Borderless modal Window with rounded corners / transparency allowed. Here's my report:
HP Quality Center's Screenshot tool doesn't recognize it as a window and thus takes its screenshots as if the window weren't there.
SnagIt utilizes a keycombo to enter the capture mode. Once stroke is hit, the popup vanishes. It reappears after the capture has ended.
Standard Windows capture works OK (Alt + Prt Scr) and captures the window as it was intended to be captured.
Next thing I tried was capturing the window with an opened dropdownlist. None of the approaches mentioned above seems to work (the last approach captures the Window the same way it did before, without the open dropdown).
As far as I have understood all the talk correctly, the only thing you can do is implement this into the applications...
I am using some custom controls one of which is a tooltip controller that can display images, so I am using th ebelow code to instantiate it:
Image newImage = Image.FromFile(imagePath);
e.ToolTipImage = newImage;
obviously could inline it but just testing at the moment. The trouble is the image is sometimes the wrong size, is there a way to set the display size. The only way I can currently see is editing the image using GDI+ or something like that. Seems like a lot of extra processing when I am only wanting to adjust display size not affect the actual image.
Once you have an image object loaded from its source, the Height and Width (and Size, and all ancillary properties) are read-only. Therefore, you are stuck with GDI+ methods for resizing it in RAM and then displaying it accordingly.
There are a lot of approaches you can take, but if you were to encapsulate that out to a library which you could reuse should this problem occur again, you'll be set to go. This isn't exactly optimized (IE, may have some bugs), but should give you an idea of how to approach it:
Image newImage = Image.FromFile(myFilePath);
Size outputSize = new Size(200, 200);
Bitmap backgroundBitmap = new Bitmap(outputSize.Width, outputSize.Height);
using (Bitmap tempBitmap = new Bitmap(newImage))
{
using (Graphics g = Graphics.FromImage(backgroundBitmap))
{
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
// Get the set of points that determine our rectangle for resizing.
Point[] corners = {
new Point(0, 0),
new Point(backgroundBitmap.Width, 0),
new Point(0, backgroundBitmap.Height)
};
g.DrawImage(tempBitmap, corners);
}
}
this.BackgroundImage = backgroundBitmap;
I did test this, and it worked. (It created a 200x200 resized version of one of my desktop wallpapers, then set that as the background image of the main form in a scratch WinForms project. You'll need using statements for System.Drawing and System.Drawing.Drawing2D.
In Winforms, if you contain the image inside a PictureBox control, the PictureBox control can be set to zoom to a particular height/width, and the image should conform.
At least that's what happened in my Head First C# book when I did the exercise.