Draw offscreen portion of form to bitmap - c#

I have a program that's goal is to digitally fill out a paper form, the users fill out the Windows form which takes all that data and fills in textboxes placed over a full size image of the form. My goal is to have it capture that entire form with all the values placed on it and store it as an image.
The problem is the form is to large for most screens and the bottom portion of it goes off-screen so when it's captured it's missing the bottom portion. I've tried screenshotting it and am currently using the follow code to draw it directly to a bitmap with no luck.
using (var bitmap = new Bitmap(this.Width, this.Height))
{
this.DrawToBitmap(bitmap, new Rectangle(0, 0, bitmap.Width, bitmap.Height));
bitmap.Save(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\imagetest.jpeg");
}
I'm at a point I may consider another solution to fill out the form such as capturing the remaining portion and splicing it with the top but I feel like their must be a better way about it.
Any suggestions on capturing a form that's not drawn?

It sounds like you are trying to almost screen cap the filled out form values on top of the form as an image background? (Correct me if I'm wrong). That is not the best way to go about it, what you would want to do is have the user fill out all the required fields of the form and when they hit the submit button, save all of the form values as strings.
Then you would want to load the image of the form into memory in the form of a Bitmap object. Then just use the DrawString method to draw the strings from the form at the correct x,y coordinates on the image in memory.

Related

Save Image in PictureBox with overlapping Controls

I am developing an application in which user can select an image in a picture box.
After that he can right click on the image and add a user control which will again display an Image along with some text. This user control can be added any number of times.
User can also re-position the user controls as per need.
All this functionality has been implemented and is working fine.
Now, the requirement is to save the Image along with the user control.
Above you can see the complete image which needs to be saved. Back image is the picture box image and the user control (small images with text).
When user will click on save button the image should get saved on his disk as a single image.
This is a windows application developed in C#.
I want to know that whether this functionality can be achieved or not. If yes, then please guide me in the right direction.
If you create a copy of the bitmap then with the Graphics.DrawImage() you can draw those images onto it. You need to calculate the position of those controls.
Look here for DrawImage: http://msdn.microsoft.com/en-us/library/42807xh1.aspx
example:
Bitmap copy = new Bitmap(OriginalBitmap);
Graphics g = Graphics.FromImage(copy);
g.DrawImage(arrowBitmap, new Point(..));
copy.Save(...);
A very simple and straight forward solution exists, has been thought of by Microsoft and includes these steps:
Instead of PictureBox use a Panel and instead of using the Image property of the PictureBox use the BackgroundImage property of the Panel
note: By using also the BackgroundImageLayout property you can quite easily instruct the Panel to stretch, center or zoom the image (I'm presuming the default value which is tile is not a good option in your case)
Instead of placing the other user controls at higher Z order but alongside the previous PictureBox place them inside the Panel
Use the Control.DrawToBitmap method like so:
private void button1_Click(object sender, EventArgs e) {
var bmp = new Bitmap(this.panel1.Width, this.panel1.Height);
this.panel1.DrawToBitmap(bmp, new Rectangle(Point.Empty, bmp.Size));
bmp.Save(#"D:\test.png", ImageFormat.Png);
}
That will result in your controls begin rendered along with the picture:
Furthermore, and if your scenario allows it, you could simply use the DrawToBitmap method with any control which contains all of the actors you wish to render, for instance the actual Form.

Overlay of two images - Control which part is in the foreground with mouse coursor

I need to be able to compare two different Images in the following way:
The images are displayed on top of each other so first there is only one image visible. When hovering with the mouse coursor over the image this defines the X-position of a vertical line which is splitting the image, displaying part of the first image left to the line and the rest of the second image right to the line.
This basically should be used as a quality comparison for two images with identical contents.
Here is a picture that hopefully makes my intentions clear:
you can use splitter control. splitter one side you put one picture control another side put other picture control
I was able to resolve this by using a SplitContainer and custom drawing. As described in the comment of Vasanthakumar's answer purely using a picturebox is not enough as you will not be able to align the right image at the same starting point than the left image (top left of the form).
What I did was the following:
pictureBox 1 displays its image normally
the Image in pictureBox 2 is drawn on every move of the splitter (this.splitContainer1.SplitterMoved += new System.Windows.Forms.SplitterEventHandler(this.splitContainer1_SplitterMoved); with a custom subset of the Image to be displayed.
This effectively allows to generate the overlay I need.
Part of my implementation showing the drawing logic:
Bitmap bmp = new Bitmap(gImg2.Width, gImg2.Height);
using (Graphics g = Graphics.FromImage(bmp))
{
g.DrawImage(gImg2, 0, 0, new Rectangle(e.SplitX, 0, gImg2.Width - e.SplitX, gImg2.Height), GraphicsUnit.Pixel);
}
pictureBox2.Image = bmp;

Showing image / text on screen without forms in c#

How can I show something on screen wihtout using forms?
Actually, I want to show some text and images popping on screen.
EDIT: just like i said HERE
What you can do is to create a alphatransparent form that draws the PNG and position it at the correct location and bind move etc.
http://www.codeproject.com/KB/GDI-plus/perpxalpha_sharp.aspx
PerPixelAlphaForm transparentImageForm = new PerPixelAlphaForm();
transparentImageForm.SetBitmap(<IMAGE GOES HERE>,<OPACITY GOES HERE>);
//opacity is the opacity that the image will be drawn with, a value of 255 = all transparent parts will be alpha/transparent just as much as the original PNG etc..
and you can put a timer that calls SetBitmap and changes the Opacity to fade in/out the image on the screen
And you can generate the text (on the fly) in a nice way with code from THIS article
and pass that image to the SetBitmap of the AlphaTransparent form.
EDIT: OR GO TO WPF.
I don't think you can: a form is equivalent to a window, and Windows applications draw into a window and not directly onto the screen (unless perhaps it's the backgroun/wallpaper, which I don't know about).
I think that's overdoing it but XNA will allow you to draw to the screen, but it is meant to be used for games so you will have trouble fitting it to a normal application.

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 ?

c#: Getting the image behind a control

I have c# form which have several controls on it, part of the controls are located one on another. I want a function that will take for input a control from the form and will return the image that has to be behind the control. for ex: if the form has backgroundimage and contains a button on it - if I'll run this function I'll got the part of backgroundimage that located behind the button. any Idea - and code?
H-E-L-P!!!
It may be simpler to use an image map http://www.echoecho.com/htmllinks07.htm
You can create your links using Page.ClientScript.GetCallback...
Edit: Winforms solution
This takes the background image of 'this' which you can change to your form object and copies the whatever is at the same position and size of your button to a bitmap object.
Graphics objGraphics = Graphics.FromImage(this.BackgroundImage);
Bitmap objBitmap = new Bitmap(button1.Size.Width, button1.Size.Height);
objGraphics.DrawImage(bitmap, new Point(0, 0), new Rectangle(button1.Location, button1.Size), GraphicsUnit.Pixel);

Categories