Graphics in C# versus Graphics in Java
-
Hello guys and girls I'm recently started on C# but as I know abit programming (Coming from Java) it should be easy to do this, but as I have experienced from Microsoft its never easy on customizing or subclassing their components (I've tried in Win32/C Old school, but thats many years ago) The question is: I want to draw a dropshadowborder on a panel and/or take it further to draw all components with a dropshadow when they are added to the panel I started on subclassing Panel to override OnPaint, but as I cannot do the following code (atleast not yet) in C# I would like to have an explanation if you guys/girls are up for it or willing to help me out so I learn abit more this day // Sample code working in Java
@Override
public void paint(Graphics g) {
if (shadow == null) {
BufferedImage buffer = new BufferedImage(getWidth(),
getHeight(),
BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = buffer.createGraphics();
super.paint(g2);
shadow = factory.createShadow(buffer);
g2.dispose();
g.drawImage(shadow, distance_x, distance_y, null);
g.drawImage(buffer, 0, 0, null);
} else if (isVisible()) {
g.drawImage(shadow, distance_x, distance_y, null);
super.paint(g);
}
}// End of sample snippet My snippet in C#: //Snippet from C#
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
Image buffer = new Bitmap(Width, Height);
Graphics g2 = Graphics.FromImage(buffer); // Here I want to take a "snapshot" of the panel it self or the components added to the panel but cant quite see where I shall steal the "image" I know atm this is "white"
g2.FillRectangle(new SolidBrush(Color.Black), new Rectangle(5, 5, Width, Height));
e.Graphics.DrawImage(buffer, 0, 0);
}I know the code convertion isnt exact the same but it was to point out my problem I hope you can see what I want to do, and I hope someone is willing to spend some minutes answering me with some theory. My Best Regards and Wishes David Bundgaard
-
Hello guys and girls I'm recently started on C# but as I know abit programming (Coming from Java) it should be easy to do this, but as I have experienced from Microsoft its never easy on customizing or subclassing their components (I've tried in Win32/C Old school, but thats many years ago) The question is: I want to draw a dropshadowborder on a panel and/or take it further to draw all components with a dropshadow when they are added to the panel I started on subclassing Panel to override OnPaint, but as I cannot do the following code (atleast not yet) in C# I would like to have an explanation if you guys/girls are up for it or willing to help me out so I learn abit more this day // Sample code working in Java
@Override
public void paint(Graphics g) {
if (shadow == null) {
BufferedImage buffer = new BufferedImage(getWidth(),
getHeight(),
BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = buffer.createGraphics();
super.paint(g2);
shadow = factory.createShadow(buffer);
g2.dispose();
g.drawImage(shadow, distance_x, distance_y, null);
g.drawImage(buffer, 0, 0, null);
} else if (isVisible()) {
g.drawImage(shadow, distance_x, distance_y, null);
super.paint(g);
}
}// End of sample snippet My snippet in C#: //Snippet from C#
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
Image buffer = new Bitmap(Width, Height);
Graphics g2 = Graphics.FromImage(buffer); // Here I want to take a "snapshot" of the panel it self or the components added to the panel but cant quite see where I shall steal the "image" I know atm this is "white"
g2.FillRectangle(new SolidBrush(Color.Black), new Rectangle(5, 5, Width, Height));
e.Graphics.DrawImage(buffer, 0, 0);
}I know the code convertion isnt exact the same but it was to point out my problem I hope you can see what I want to do, and I hope someone is willing to spend some minutes answering me with some theory. My Best Regards and Wishes David Bundgaard
The control has a method that causes it to render itself to a bitmap. It's called DrawToBitmap. Use that to create a copy of your control in a bitmap, then you can manipulate it.
Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.