|
thanks
sorry for the stupid question
this is something i could have figured out for myself
|
|
|
|
|
Hello.
I have finished graphical component. It uses GDI+. I understand that I have to dispose unmanaged resources (like Pen, Brush, etc).
But one question arised. How I should dispose global resources?
I`ve decided to use global Pen because this component repaints every 100 ms (but maybe I made wrong decision).
Example of my code:
public partial class MyControl: UserControl
{
Pen myPen;
// constructor
public MyControl
{
...
myPen = new Pen(Color.Blue);
...
}
}
|
|
|
|
|
I don't know C#, but from the looks of your code sample, I don't see anything global, nor
do I see any unmanaged objects.
myPen's existence lasts as long as a MyControl object exists, right?
Am I missing something?
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Hi,
you should call Dispose on those objects that offer it, as soon as you don't need them
anymore. Creating and disposing GDI objects may be expensive, so it is indeed wise
to avoid these operations in every OnPaint(). So you create them once "globally",
and keep them alive as long as the app lives; that is perfect. On exit, the system
will take care of them (as it always did with resources, even long before .NET came
into this world).
BTW: you don't have the right to dispose of stuff you did not create or ordered created;
this applies to the Pens class, the Brushes class, etc.
So you can replace myPen = new Pen(Color.Blue); by myPen=Pens.Blue;
and not worry about disposing of it at all.
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.
|
|
|
|
|
Thanks you very much !
I'm new in C# Maybe that explains a lot.
I was just worried about way of disposing things in GDI+.
1 more question (please :
If I create the class (component) with these "global" Pens (or whatever) and after some time I don't
need this class (for example - app will change), should I implement Dispose method?
And sorry for my english
|
|
|
|
|
Hi,
your class XXX should implement Dispose() when it uses unmanged memory or other system
resources, because you either do native calls to that effect, or use objects that offer
a Dispose() themselves.
the above holds true in general; you could forget about providing Dispose() if the
class is used in one app only, where you need all objects of class XXX up to the end
of the app execution (so you would call Dispose only on exit, which is not that useful
since the system would clean up then anyway).
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.
|
|
|
|
|
Thank you again (especially for quick response)
|
|
|
|
|
Hi Guys
Is it possible to create a usercontrol with a semi-transparent background (without affecting and not relying on the parent form’s properties).
I can make the backcolor of the control transparent but I have to set the transparency key in the parent form for it to work but the form also renders out a transparent background !!!.
Here is the code i'm using inside the Usercontrol
<br />
private void DrawImageBox()<br />
{<br />
SetStyle(ControlStyles.SupportsTransparentBackGround, true);<br />
base.BackColor = Color.Transparent;<br />
<br />
if (Picture != null)<br />
{<br />
Graphics gfx = base.CreateGraphics();<br />
<br />
<br />
Bitmap bmp = new Bitmap(Picture);<br />
bmp.MakeTransparent( _transparentKey );<br />
<br />
Rectangle rec = Rectangle( 0, 0, base.Width, base.Height );<br />
<br />
gfx.DrawImage( bmp, rec, 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel);<br />
<br />
gfx.Dispose();<br />
}<br />
<br />
}<br />
If anyone has any ideas for how to do this I would be really grateful!
Thanks in advance!
|
|
|
|
|
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++
|
|
|
|