|
Hi, the short answer to your question is no. The win32 system does not support semi-transparent controls in the way you want it. However, considering that you're UserControl is painting an image, there is an alternative way - as long as you're only painting over other graphics & not over controls (buttons, textboxes etc.). Any image you want to paint can be adjusted so that it's semi-transparent. Ditch the UserControl & paint the image from the main form by overriding the OnPaint method. Here's the code to adjust the image transparency. You'll need to enable the 'Allow unsafe code' option in the project Build properties.
private Bitmap _transImage = null;
public Form1()
{
InitializeComponent();
}
~Form1()
{
_transImage.Dispose();
}
protected override void OnLoad(EventArgs e)
{
Bitmap b = new Bitmap(@"c:\myImage.png");
_transImage = AdjustImageTransparency(b);
b.Dispose();
base.OnLoad(e);
}
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.DrawImage(_transImage, new Rectangle(40, 40, _transImage.Width, _transImage.Height));
base.OnPaint(e);
}
private Bitmap AdjustImageTransparency(Bitmap sourceImage)
{
Bitmap transImage = new Bitmap(sourceImage.Width, sourceImage.Height, PixelFormat.Format32bppArgb);
Rectangle rc = new Rectangle(Point.Empty, sourceImage.Size);
using (Graphics g = Graphics.FromImage(transImage))
g.DrawImage(sourceImage, rc);
BitmapData bd = transImage.LockBits(rc, ImageLockMode.ReadWrite, transImage.PixelFormat);
AdjustTransparency(bd, 0.5f);
transImage.UnlockBits(bd);
return transImage;
}
private unsafe void AdjustTransparency(BitmapData dataObj, float ratio)
{
int byteWidth = dataObj.Width * 4;
for (int y = 0; y < dataObj.Height; y++)
{
byte* row = (byte*)dataObj.Scan0 + dataObj.Stride * y;
for (int x = 0; x < byteWidth; x++)
{
if ((x % 4) == 3)
{
float val = (float)row[x];
val *= ratio;
val = Math.Min(Math.Max(val, 0), 255);
row[x] = (byte)val;
}
}
}
}
also, add the System.Drawing.Imaging import to your code.
Hope this helps!
Rob
"An eye for an eye only ends up making the whole world blind"
modified on Friday, April 18, 2008 3:58 PM
|
|
|
|
|
I've chatted to a few people who refuse to use GDI+ due to it's lack of "Flashy Effects" - Lighting, Tinting, Etc, Etc, Etc
So, I thought I'd ask if anyone knows how to do something... "Flashy" in GDI+...
A Fire Effect maybe? (Think Fantastic Flame Screensaver)
Get your creativity juices flowing
- Reelix
|
|
|
|
|
There are some articles on GDI+ with some flashy stuff in them. Your friends are stupid. Flashy effects is not what GDI+ is for.
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
i need some tips on template matching using C# . For the template matching, what concepts that need to be used and how?????
pixel by pixal or some other thecnique?? i want to detect the face in an image...
what i want to do is .
http://www-cs-students.stanford.edu/~robles/ee368/matching.html
now how can i match the template image to the spacific region???
(Jameel)
|
|
|
|
|
I am using TextRender.DrawText to add text to a Bitmap but the quality is not very good. I looked for a formatting hint in TextFormatFlags but I could not find one. I know that I could use Graphics.DrawString but the performance is not as good. What do I need to do so TextRender.DrawText creates high quality text?
Thanks
Dan R.
|
|
|
|
|
Wow! that is rubbish I'd stick to .DrawString, or if you're after performance, use the DrawText win32 import
"An eye for an eye only ends up making the whole world blind"
|
|
|
|
|
Hellow
would you please help me to erase a line in C#.Here I used Drawline(x1,y1,x2,y2) method to draw a line in a panel.I need to create a tool just like in MS paint to erase it.Is there any way to do like that.I tried to erase with by drawing a rectangle with back ground color same to the panel...but its not working fine.
Here is my e-mail ID --subinraju007@gmail.com
|
|
|
|
|
There are many ways to accomplish this, but it seems to me that the easiest way is for you to redraw your image without the line being drawn. A common approach is to implement your drawing as a set of objects that are responsible for drawing themselves, then you just call redraw using these objects, so if you don't want the line to be redrawn you just omit the call to this object.
|
|
|
|
|
The code of WMSplitter studied:
the Input pin read data by StreamOnAsyncReader::Read,which is derived from IStream,
then sample is sent to the Output pines by WMFDemuxFilter::OnStreamSample,which is derived from IWMReaderCallbackAdvanced and IWMReaderCallback.
I don't know what things being done during the data from IStream::Read to IWMReaderCallbackAdvanced::OnStreamSample.
Can I debug it step by step?
Hi,Markliu-codeproject
|
|
|
|
|
Hello all,
I seem to have a problem creating bitmaps with alpha with the following code:
BITMAPINFO tBMI;
ZeroMemory (&tBMI, sizeof tBMI);
tBMI.bmiHeader.biSize = sizeof tBMI.bmiHeader;
tBMI.bmiHeader.biWidth = nSizeX;
tBMI.bmiHeader.biHeight = -nSizeY;
tBMI.bmiHeader.biPlanes = 1;
tBMI.bmiHeader.biBitCount = 32;
tBMI.bmiHeader.biCompression = BI_RGB;
HBITMAP hBmp = CreateDIBitmap(hdc, &tBMI.bmiHeader, CBM_INIT, pData, &tBMI, DIB_RGB_COLORS);
All works fine under normal circumstances, but if it happens that the data are completely transparent (all pixels have alpha set to 0), the behavior suddenly changes and instead of a completely transparent bitmap, the function chooses to ignore the alpha channel and creates a bitmap filled with black color (presumably because the RGB channels were set to 0 in the original data).
Is this some Microsoft hack to maintain compatibility or what? Is there a way to prevent it? Thanks for any hints.
|
|
|
|
|
I know people argue with me here about this, but there really is very little
support for ARGB in the old GDI bitmaps. Some functions may honor the alpha
channel but many ignore it.
The exception is DIBSections...
Try CreateDIBSection instead - I think you'll find much more consistent
results with your 32bpp ARGB bitmaps
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Thanks for the answer. I have tried it, but unfortunately, the same "heuristics" applies to CreateDIBSection as well.
I ended up using CreateIconFromResourceEx & DrawIconEx combination. Sounds a bit strange for such a "simple" task as displaying a RGBA image data...
|
|
|
|
|
Vlasta_ wrote: but unfortunately, the same "heuristics" applies to CreateDIBSection as well.
Hmm, I guess that depends on how you display the bitmap. There's not many choices for
that in GDI with ARGB data either.
Vlasta_ wrote: I ended up using CreateIconFromResourceEx & DrawIconEx combination.
Ok, whatever works I personally prefer to use GDI+ for ARGB over something like that.
Cheers,
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Agreed, I'm using GDI+ for 32bpp bitmaps in a project now and it's worked just fine.
|
|
|
|
|
Hmmm... I'd like to have the applications compatible with many Windows versions (including Windows 98 and NT4). It is OK, if things look ugly on the older versions, but the application should run.
So, I was drawing using an imagelist with this single bitmap. Maybe the optimization was inside the image list.
|
|
|
|
|
gdiplus could do it, but...it would increase the size of the installer too much for just this simple task. and who knows, maybe it will have the same problem (it is built upon the classic gdi, isn't it?)
besides I like agg better for the vector stuff
...but thanks for the recommendation, it is a good advice, my needs are just a bit special.
|
|
|
|
|
Vlasta_ wrote: it is built upon the classic gdi, isn't it?
No. It is an extension. It does a lot of stuff GDI does not.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
As long as you found a solution, there's nothing wrong with that!
As for the imagelist, maybe a ILC_COLOR32 imagelist would work, since that uses a
dibsection internally, but I don't see any support to render an image from the
imagelist using the alpha channel values.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
yes, I was using ILC_COLOR32 and it worked correctly in all cases except when every pixels' alpha was 0. So the weird behavior only appeared in this single case, it was enough to change the alpha of the first pixel to 1 and it suddenly started to work as expected. That's why I was calling it "optimization" .
I believe it is worth mentioning somewhere in the documentation... something like: BTW, dear Windows user if you create completely transparent 32-bits bitmap, we will ignore the alpha channel.
|
|
|
|
|
Vlasta_ wrote: That's why I was calling it "optimization"
Not a very optimal optimization.
What function were you using to draw the bitmap? I'd like to play with that a bit.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
With ImageList_Draw.
Hm, "optimization" is not a good word. Older Windows did not support alpha at all and when the support was introduced, the functions were probably extended to handle it. But, I assume, due to compatibility issues someone decided to incorporate this "optimization" and when 32bit bitmap is used and it is completely transparent, it kicks in.
|
|
|
|
|
Hello,
i am writing a 3d program using DirectX. Now i want to play a piece of sound with DirectSound, but the initilization was not accomplished.
the code i have written below,
....
CoInitialize(NULL);
if (FAILED( hResult = CoCreateInstance(
CLSID_DirectMusicLoader ,
NULL,
CLSCTX_INPROC,
IID_IDirectMusicLoader8,
(void**) &pLoader)))
{
return SOUNDERROR_MUSICLOADER;
}
if (FAILED( hResult = CoCreateInstance(
CLSID_DirectMusicPerformance,
NULL,
CLSCTX_INPROC,
IID_IDirectMusicLoader8,
(void**) &pPerformance)))
{
return SOUNDERROR_MUSICPERFORMANCE;
}
....
the initial of DirectMusicLoader succeed,
but wenn DirectMusicPerformance is initilized, it returned failure.
Does anyone knows how can i solve the problem?
thx anyway.
mwolf122
|
|
|
|
|
Hey guys
im having a problem getting DoubleBuffer to work.
on my form i have a panel on which i dynamically draw a time line with intervals, arrows, arrowheads ect. The panel is docked to the form, when u resize the form the panel resizes with it and then redraws the time line.
now all that works perfect but the image flickers as i resize the form, i searched through some articles to see how to enable double buffering and did just that... but the flicker is still there
my forms constructor is as follows
public Form1()
{
InitializeComponent();
canvas = new Rectangle(0, 0, panel1.Width, panel1.Height);
calcVariables();
SetStyle(ControlStyles.AllPaintingInWmPaint |
ControlStyles.UserPaint |
ControlStyles.DoubleBuffer |
ControlStyles.ResizeRedraw,
true);
}
what am i doing wrong?
i can e-mail u the demo project if it wil help
thanx
Harvey Saayman - South Africa
Junior Developer
.Net, C#, SQL
think BIG and kick ASS
you.suck = (you.passion != Programming)
|
|
|
|
|
Hi Harvey,
IIRC the SetStyles way of getting DoubleBuffered works as is provided you:
- either do it before the Control becomes visible (or has its handle created?);
- or apply Control.UpdateStyle() after the SetStyles stuff.
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips:
- before you ask a question here, search CodeProject, then Google;
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get;
- use PRE tags to preserve formatting when showing multi-line code snippets.
|
|
|
|
|
stil nothing...
i now have this as my constructor..
public Form1()
{
SetStyle(ControlStyles.AllPaintingInWmPaint |
ControlStyles.UserPaint |
ControlStyles.DoubleBuffer |
ControlStyles.ResizeRedraw,
true);
InitializeComponent();
canvas = new Rectangle(0, 0, panel1.Width, panel1.Height);
calcVariables();
UpdateStyles();
}
any ideas?
Harvey Saayman - South Africa
Junior Developer
.Net, C#, SQL
think BIG and kick ASS
you.suck = (you.passion != Programming)
|
|
|
|
|