Tuesday, March 27, 2007

En los foros del Guille (http://foros.elguille.info)
recientemente se pregunto en dos post como se podría salvar la imagen de un
PictureBox con todo y controles añadidos. Al principio se sugirió utilizar
la clase Graphics y dibujar nosotros mismos la apariencia del control teniendo
en cuenta el tipo: Label, PictureBox, etc. El código necesario para esto se
hace muy engorroso a medida que necesitamos dibujar más y más controles (por no
mencionar el hecho de dibujar mediante Graphics un TabControl…). Aquí dejo una
manera más sencilla:




using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace Temp
{
class PictureBoxEx : PictureBox
{
public Image GetCompleteImage()
{
Bitmap bitmap = this.Image.Clone() as Bitmap;

for (int i = this.Controls.Count - 1;i>=0 ; i--)
{
this.Controls[i].DrawToBitmap(bitmap,this.Controls[i].Bounds);
}
return bitmap;
}
}
}




Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Windows.Forms
Imports System.Drawing

Namespace Temp
Class PictureBoxEx
Inherits PictureBox
Public Function GetCompleteImage() As Image
Dim bitmap As Bitmap = TryCast(Me.Image.Clone(), Bitmap)
For i As Integer = Me.Controls.Count - 1 To 0 Step -1

Me.Controls(i).DrawToBitmap(bitmap, Me.Controls(i).Bounds)
Next
Return bitmap
End Function
End Class
End Namespace



En este fragmento, creamos una
clase que extiende de PictureBox, añadiendo el método GetCompleteImage. Donde
se crea una copia de la imagen de fondo y se comienza a iterar a través de la
propiedad Controls de tipo ControlColecction. Obsérvese que se itera desde el
último elemento hasta el primero, teniendo el cuenta el Z-order (a los que han
trabajado con divs y css les debe sonar, y claro WPF ;-) ), el mismo esta
determinado por el índice del control en la colección. Posteriormente
utilizamos el método DrawToBitmap existente en la clase control, pasándole el
bitmap donde dibujaremos y el área donde se pintara  dentro del mismo.

3 comments:

Anonymous said...

Does anyone discern of an accurately how many countries are ration in return Haiti and what countries they are? Peradventure anything else?
[url=http://avida-ed.msu.edu/cgi-bin/mwf/topic_show.pl?tid=14220]car acoustics[/url] car audio, acoustics system
[url=http://avida-ed.msu.edu/cgi-bin/mwf/topic_show.pl?tid=14245]buy lighters[/url] zippo lighters, bic products

[url=http://nails.getenjoyment.net]Nails[/url] , [url=http://nails.getenjoyment.net/sitemap.html]Nails[/url] [url=http://nails.getenjoyment.net/taking-off-acrylic-nails.html]Taking misguided acrylic nails[/url]
I through services like sites above... Who can pirate?
Thanks a lottery

Anonymous said...

Does anyone know of an accurately how various countries are ration for Haiti and what countries they are? Dialect mayhap anything else?
[url="http://avida-ed.msu.edu/cgi-bin/mwf/topic_show.pl?tid=14220"]car acoustics[/url] car audio, acoustics system
[url="http://avida-ed.msu.edu/cgi-bin/mwf/topic_show.pl?tid=14245"]buy lighters[/url] zippo lighters, bic products
[url="http://avida-ed.msu.edu/cgi-bin/mwf/topic_show.pl?tid=18099"]shaadi[/url] muslim shaadi, mujhse shaadi karogi
[url="http://avida-ed.msu.edu/cgi-bin/mwf/topic_show.pl?tid=18100"]mother of the bride dresses[/url] plus size discounted mother of the bride dresses, mother of the bride dresses with sleeves
[url="http://avida-ed.msu.edu/cgi-bin/mwf/topic_show.pl?tid=18095"]pyramat[/url] pyramat g-flex sound booster portable audio chair, pyramat gaming chair
[url="http://avida-ed.msu.edu/cgi-bin/mwf/topic_show.pl?tid=18096"]sherpani[/url] sherpani luna backpack, sherpani bag
[url="http://avida-ed.msu.edu/cgi-bin/mwf/topic_show.pl?tid=18097"]STOKKE HIGH CHAIR[/url] stokke tripp trapp high chair, tripp trapp
[url="http://avida-ed.msu.edu/cgi-bin/mwf/topic_show.pl?tid=18098"]Tolomeo desk lamps[/url] api executive desk lamps, api antique executive desk lamps
[url="http://avida-ed.msu.edu/cgi-bin/mwf/topic_show.pl?tid=18101"]DOUBLE GLAZING WINDOW HANDLES[/url] how to install double glazing windows, double glazing window hinges
[url="http://avida-ed.msu.edu/cgi-bin/mwf/topic_show.pl?tid=18102"]tuff shed[/url] tuff sheds cheap, tuff shed barn deluxe

I using services like sites surpassing... Who can forbear me?
Thanks

Anonymous said...

Just wanted to say hello someplace. Found [url=http://www.google.com/ncr]you guys through google[/url]. Hope to contribute more soon!
-rireaciste