I asked:
3) The rendered image has a black background. The metafile draws text, a
box, and a line. But the metafile does not first fill the canvas with white.
So the background starts off "empty."
Question:
3) How do I have it start off with a background/fill of white instead of
black?
You said:
3) Call Graphics.FillRectangle to fill the bitmap first and then draw your
image.
My code is as follows:
System.IO.MemoryStream mem = new System.IO.MemoryStream();
img.Save(mem, System.Drawing.Imaging.ImageFormat.get_Bmp());
I have no graphics context as this runs on the server and has no UI. How can
I do a fill first?
And even if I can, the bitmap generated has black for the background - it's
not an alpha channel set to 100% transparent. So it would overwrite it
anyways I believe.
--
thanks - dave
You try to test the code below.
Since we draw a 400x400 wmf onto a 500x500 picture, it will has backcolor
of White as we specified.
Metafile mf = new Metafile(@"C:\Program Files\Microsoft
Office\CLIPART\PUB60COR\AN01173_.WMF");
private const int szImg =500;
private void button3_Click(object sender, System.EventArgs e)
{
Bitmap bmp = new Bitmap(szImg,szImg);
Graphics g = Graphics.FromImage(bmp);
SolidBrush sb = new SolidBrush(Color.White);
g.FillRectangle(sb,0,0,szImg,szImg);
g.DrawImage(mf,0,0,szImg-100,szImg-100);
bmp.Save(@"C:\Temp\DesImg.bmp",ImageFormat.Bmp);
}
Best regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
I won't be able to implement/test this till Monday but I think this gave me
the solution to all of this. I assumed that since a Bitmap could be created
with a metafile, that was the way to do it. The code you list here is a
totally different way to get a metafile into a bitmap and it seems a logical
way to do it.
--
thanks - dave
Thanks for your quickly reply!
I look forward to hearing from your.
Ok, this is working for emf/wmf except I do not want to have to use:
SolidBrush sb = new SolidBrush(Color.White);
g.FillRectangle(sb,0,0,szImg,szImg);
Because I want the original bitmap to be empty - ie a 32-bit bitmap with
alpha and the entire bitmap is 100% transparent to start. Then what I draw
from the metafile will fill part of the bitmap, but the rest will remain
transparent.
Like a sprite in a game.
How can I do this?
--
thanks - dave
The transparent color is implemented via the Alpha blending.
That is to say we need to use code to control how picture A draw on Picture
B to implement the transparent (alpha blending).
Here is a link for your reference.
Alpha Blending Lines and Fills
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
l/_gdiplus_alpha_blending_lines_and_fills_usecsharp.asp
Yes I understand all of that. But how can I start with a bitmap that is 100%
transparent? That link shows how to draw with alpha on top of a bitmap.
Here is an example. Lets say I have a sprite in a video game. Parts of the
bitmap are 100% transparent. So when I place the sprite on the game, the
parts of the bitmap rectangle that are not the sprite show what is below it.
How do I get an initial bitmap that is empty?
--
thanks - dave
There is no such a transparent bitmap, it should have the color(ARGB).
In the Game program, the code is doing alpha blend all the time.
e.g.
Background Image(A)
character Image(B), the sprite
The game will keep rendering the A+B to generating C and show it.
The C is the result Bitmap with A and B alpha blending.
Of course, in Game programming they have special technique to make the
render job fast. That is out of our topic.
--
thanks - dave
Maybe another way to ask the question is, I want to put a rectangular window
in my bitmap so that rectangle is 100% transparent. How do I do that?
--
thanks - dave
The code below will draw a 100% transparent rectangle onto the bmp.(You
will see nothing)
Bitmap bmp = new Bitmap(szImg,szImg);
Graphics g = Graphics.FromImage(bmp);
g.DrawImage(mf,0,0,szImg-100,szImg-100);
Color cl = Color.FromArgb(0,0,0,0);
//Color cl = Color.FromArgb(255,0,0,0);this way the rectangle can be seen
Pen myPen = new Pen(cl,5);
g.DrawRectangle(myPen,0,0,50,50);
bmp.Save(@"C:\Temp\DesImg.bmp",ImageFormat.Bmp);
there is no clear pixel, every pixel must has a RGB value(0<R,G,B<255)
e.g. a color of (255,255,255) is black,(0,0,0) is white.
Also I suggest take a look at a book about windows Graphics.
e.g.
Windows Graphics Programming: Win32 GDI and DirectDraw (With CD-ROM)
(Hardcover)
by Feng Yuan
http://www.amazon.com/gp/product/0130869856/104-8670888-7842303?v=glance&n=2
83155&n=507846&s=books&v=glance
I do have a bit of experience with graphics (I've done the graphics in a
number of PC games). I would state it slightly different. Each pixel must
have a specific ARGB value. And if a given pixel has A=0, then while it has
an RGB value too, they are irrelevant.
What I am trying to do is get a Bitmap object where the pixels in the bitmap
have A=0. I then want to draw the metafile on that bitmap and the end result
will be that the final bitmap will remain A=0 on pixels the metafile did not
draw to.
I tried your suggestion of:
sb = new SolidBrush(Color.FromArgb(0, 0, 0, 255)); // blue so I see it if
the alpha is off
g.FillRectangle(sb, 0, 0, 10, 10);
And that did not remove the underlying ARGB of (255, 255, 255, 255) - it was
still black. I think the FillRectangle() writes that rectangle on top of what
is already on the bitmap. As it has A=0, it basically is a NOP.
So my question returns to, How can I get pixels in a Bitmap object to have
A=0? There must be a way because otherwise you could not have non-rectangular
bitmaps - like sprites in games, bubble help pop-ups, or rounded corners on
windows.
--
thanks - dave
Thanks for your quick response.
It seems that we need do low level gdi opertation to set the pixel of A=0.
Also I find that you have a similar in the newsgroup below.
Subject: Create metafile - bitmap with alpha
Newsgroups: microsoft.public.win32.programmer.gdi
Our colleague will follow you in that thread.
Thanks for your understanding!
That thread was a very different question - it was how to take a bitmap with
alpha pixels (what I am trying to learn here) and place that in a metafile.
So my question remains, how can I get pixels in a Bitmap object to have
A=0? There must be a way because otherwise you could not have non-rectangular
bitmaps - like sprites in games, bubble help pop-ups, or rounded corners on
windows.
--
thanks - dave
Based on my research, that need to read/write the rawdata in the Bitmap and
I am not very familar with GDI issue.
I think that may need consult the GDI/GDI+ API to do that.
You may try to post it the newsgroup below, and that is why I suggest post
in the gdi newsgroup.
Newsgroups: microsoft.public.win32.programmer.gdi
Thanks!
--
thanks - dave
Thanks for your quickly reply!
Based on my knowledge, I think that is GDI issue, due to our process, the
gdi newsgroup is the right place you need to consult.
If you have any other concern about .NET framework, please feel free to
post here.
I am trying to set A=0 form managed code. Would you please ask the .net
development group how to do this to a Bitmap object? There must be a way to
do this.
--
thanks - dave
I will forward your concern.
Thanks!
Based on my research, we need to use the code below to read/write the
BitmapData directly.
private void button3_Click(object sender, System.EventArgs e)
{
Bitmap bmp = new Bitmap(szImg,szImg,PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(bmp);
SolidBrush sb = new SolidBrush(Color.White);
g.FillRectangle(sb,0,0,szImg,szImg);
sb=new SolidBrush(Color.Blue);
g.FillRectangle(sb,0,0,szImg-100,szImg-100);
BitmapData bd = bmp.LockBits(new
Rectangle(10,10,100,100),ImageLockMode.ReadWrite,bmp.PixelFormat);
for(int y=0;y<bd.Height;y++)
for(int x=0;x<bd.Width;x++)
{
Marshal.WriteByte(bd.Scan0,(bd.Stride*y)+(4*x),0);
}
bmp.Save(@"C:\Temp\DesImg.bmp",ImageFormat.Bmp);
}
Using the LockBits method to access image data
http://www.bobpowell.net/lockingbits.htm
AsI said before I am not familar with Bitmap, I am sorry for that.
Also I think for .NET Drawing issue, you may post in the newsgropu below.
That is the newsgroup discussed according class under System.Drawing.
microsoft.public.dotnet.framework.drawing
Also, no dotnet.framework.drawing in the managed newsgroups...
--
thanks - dave
Thanks for your feedback.
So far the managed newsgroup did not cover all the microsoft public
newsgroup.