C
C#6mo ago
nullptr

how can i speed up this transparent image rendering in winforms?

hello! i'll be frank; i found this code on Stack Overflow and it goes far over my head, i just need a way to layer transparent images. this is the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Aerochat.Forms
{
class TransparentPictureBox : PictureBox
{
public TransparentPictureBox()
{
this.BackColor = Color.Transparent;
}
protected override void OnPaint(PaintEventArgs e)
{
if (Parent != null && this.BackColor == Color.Transparent)
{
using (var bmp = new Bitmap(Parent.Width, Parent.Height))
{
Parent.Controls.Cast<Control>()
.Where(c => Parent.Controls.GetChildIndex(c) > Parent.Controls.GetChildIndex(this))
.Where(c => c.Bounds.IntersectsWith(this.Bounds))
.OrderByDescending(c => Parent.Controls.GetChildIndex(c))
.ToList()
.ForEach(c => c.DrawToBitmap(bmp, c.Bounds));

e.Graphics.DrawImage(bmp, -Left, -Top);

}
}
base.OnPaint(e);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Aerochat.Forms
{
class TransparentPictureBox : PictureBox
{
public TransparentPictureBox()
{
this.BackColor = Color.Transparent;
}
protected override void OnPaint(PaintEventArgs e)
{
if (Parent != null && this.BackColor == Color.Transparent)
{
using (var bmp = new Bitmap(Parent.Width, Parent.Height))
{
Parent.Controls.Cast<Control>()
.Where(c => Parent.Controls.GetChildIndex(c) > Parent.Controls.GetChildIndex(this))
.Where(c => c.Bounds.IntersectsWith(this.Bounds))
.OrderByDescending(c => Parent.Controls.GetChildIndex(c))
.ToList()
.ForEach(c => c.DrawToBitmap(bmp, c.Bounds));

e.Graphics.DrawImage(bmp, -Left, -Top);

}
}
base.OnPaint(e);
}
}
}
i seriously don't understand what its doing, nor why it's so slow, nor why this works but the regular PictureBox doesn't with layered transparent images. there are only a few of them on the form..
17 Replies
Pobiega
Pobiega6mo ago
winforms doesnt support transparency, and this seems like a very hacky workaround with manually re-rendering every single overlapping control as an image. I'd strongly recommend just using a more modern UI framework that supports transparency
nullptr
nullptrOP6mo ago
like what? i really like the intuitive way winforms works
Pobiega
Pobiega6mo ago
Avalonia comes to mind or WPF I'm honestly not very well versed with GUI, its not my cup of tea, but winforms is old and has problems, including specifically transparency
nullptr
nullptrOP6mo ago
i did start a wpf port but i got stuck porting a specific control. any chance u could help?
Pobiega
Pobiega6mo ago
Nope, sorry. As said, I'm not a GUI person. Maybe try asking in #gui
nullptr
nullptrOP6mo ago
alright, thanks
Denis
Denis6mo ago
Which control?
MODiX
MODiX6mo ago
nullptr
hello, i've been working on porting an existing project to wpf but i don't really know how i'd implement this control that i wrote (too much text, i'll send it in a seperate message). all it does is take an image (see the first attachment for an example) and applies it as a border, given an InsetPixels value which determines which pixels not to stretch (see the second attachment, look at the form border; that's the image)
Quoted by
<@1053012491006910504> from #gui (click here)
From nullptr
React with ❌ to remove this embed.
Denis
Denis6mo ago
Stack Overflow
How to Use an image as ImageBrush on a Line or Path in WPF
I have a Canvas on which I draw lines (via Lines or Paths) that indicates a road to follow for users. Is there a way to change the line so that it displays a repetitive image instead? What I'm look...
nullptr
nullptrOP6mo ago
the issue is, i don't just want a border pattern, the images usually have drop shadows and stuff, and effects inside the border that need to be stretched whilst preserving the outside the old code does this perfectly
nullptr
nullptrOP6mo ago
nope. i'm trying to draw transparent images on top of each other
Shazanman
Shazanman6mo ago
alpha blending?
nullptr
nullptrOP6mo ago
yes, i guess
Shazanman
Shazanman6mo ago
How to: Use Compositing Mode to Control Alpha Blending - Windows Fo...
Learn how to use compositing mode to control alpha blending using a System.Drawing.Bitmap object and a System.Drawing.Graphics object.
Shazanman
Shazanman6mo ago
skiasharp also comes into mind which supports porter duff modes
Want results from more Discord servers?
Add your server