Re: Bitmap + Clipboard + Transparency = Blue Background?
- From: "Christopher Ireland" <cireland@xxxxxxxxx>
- Date: Wed, 16 Apr 2008 11:21:47 +0200
Michael,
Thank you again for your help, Michael. I really feel I'm making progress in
this area thanks to you.
Not all of the image formats that are supported by gdiplus allow for
alpha channel images.
I don't suppose you have a list (or a link) to the image formats for which
gdiplus allows alpha channels? From the tests I've made here on the image
formats I'm interested in, PNG, EMF and TIFF save with alpha channels in
tact whereas GIF and JPG do not.
For non alpha channel image formats, you should convert the image to
a non alpha channel format before placing it on the clipboard.
Yes, that's pretty straightforward to do.
Just because the Operating System supports a clipboard format does
not mean that a particular application will use it.
We are talking about pasting images into documents here, where both the
operating system and the documents are written by the same software company.
I won't labour the point any more than that :-)
The best way to determine what an application supports is to copy an
image from that application to the clipboard and then examine the
supported clipboard formats with the clipboard viewer utility.
Ok, I insert one of the correctly formed transparent PNG images from my
tests into a Word2003 document. The transparency is preserved correctly
within the Word2003 document. I select the image and copy it. The "View"
menu within ClipBook Viewer shows me a long list that includes the "PNG"
format but only the "Enhanced Metafile, Picture, DIB Bitmap and Bitmap"
formats are written in black text. All the other formats, including the
"PNG" format, are written in grey text. I've had a look in the ClipBook
Viewer help file looking for an explanation of these greyed out formats, but
can't find one. Do you have any idea?
I also don't understand why I can copy a transparent PNG, EMF or TIFF file
by selecting it in Windows Explorer and then using Ctrl+C and have it
successfully paste into a Word2003 document with its transparency preserved
but when I use c# to do the same it doesn't work. When I look in ClipBook
Viewer having copied a file from Windows Explorer it tells me that the
format is "Drag-Drop Data", but using DataFormats.FileDrop when I set the
data to the clipboard in c# doesn't work in the same way.
Another difficulty I am experiencing is being able to copy EMF files to the
clipboard at all using c#. I include a full code example below. In the code
below, GetDataPresent returns true but the MemoryStream strm is always
false. I know I am being very demanding on your time and patience, but if
you could take a quick look at the code I would be very grateful.
namespace WindowsFormsApplication1
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
this.BackColor = Color.Yellow;
InitializeControl();
}
private void InitializeControl()
{
myControl1 = new MyControl();
myControl1.Location = new System.Drawing.Point(40, 12);
myControl1.Size = new System.Drawing.Size(600, 400);
Controls.Add(myControl1);
myControl1.BackColor = Color.Transparent;
myControl1.AString = "My Control";
myControl1.CopyToClipboard();
myControl1.Save(@"C:\temp\mycontrol.emf");
System.Windows.Forms.IDataObject iData = Clipboard.GetDataObject();
if (null != iData)
{
if (iData.GetDataPresent(DataFormats.EnhancedMetafile, false))
{
MemoryStream strm =
(MemoryStream)iData.GetData(DataFormats.EnhancedMetafile);
if (null != strm)
{
Image image = (Bitmap)Image.FromStream(strm, true);
image.Save(@"c:\temp\mycontrol_pasted.emf", ImageFormat.Emf);
}
}
}
}
private MyControl myControl1;
}
public class MyControl : Control
{
public MyControl()
: base()
{
SetStyle(ControlStyles.OptimizedDoubleBuffer, false);
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
}
protected override void OnPaint(PaintEventArgs e)
{
Draw(e.Graphics);
}
public void Draw(Graphics g)
{
Rectangle rect = ClientRectangle;
rect.Inflate(-1, -1);
g.DrawString(AString, new Font("Verdana", 12), Brushes.Black, new
PointF(10, 10));
g.DrawRectangle(Pens.Red, rect);
}
public Metafile Metafile(Graphics gRef, Stream stream)
{
IntPtr hDC = gRef.GetHdc();
Metafile mf = new System.Drawing.Imaging.Metafile(stream, hDC);
gRef.ReleaseHdc(hDC);
gRef.Dispose();
gRef = Graphics.FromImage(mf);
try
{
Draw(gRef);
}
finally
{
gRef.Dispose();
}
return mf;
}
public string AString { get; set; }
public void CopyToClipboard()
{
MemoryStream ms = new MemoryStream();
Save(ms);
IDataObject dataObject = new DataObject();
dataObject.SetData(DataFormats.EnhancedMetafile, false, ms);
Clipboard.Clear();
Clipboard.SetDataObject(dataObject, false);
}
public void Save(string path)
{
FileStream fs = File.Create(path);
Save(fs);
}
public void Save(Stream stream)
{
Bitmap bitmap = new Bitmap(1, 1, PixelFormat.Format24bppRgb);
Graphics gRef = Graphics.FromImage(bitmap);
Metafile meta = Metafile(gRef, stream);
stream.Flush();
stream.Close();
}
private ImageCodecInfo Encoder
{
get { return GetEncoderInfo(Format.Guid); }
}
private ImageFormat Format
{
get { return System.Drawing.Imaging.ImageFormat.Emf; }
}
private ImageCodecInfo GetEncoderInfo(Guid g)
{
ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders();
for (int t = 0; t < encoders.Length; t++)
{
if (encoders[t].FormatID == g)
return encoders[t];
}
return null;
}
private EncoderParameters EncoderParams
{
get { return null; }
}
}
}
--
Thank you,
Christopher Ireland
http://shunyata-kharg.doesntexist.com
"The ultimate authority must always rest with the individual's own reason
and critical analysis."
Dalai Lama
.
- Follow-Ups:
- Re: Bitmap + Clipboard + Transparency = Blue Background?
- From: Michael Phillips, Jr.
- Re: Bitmap + Clipboard + Transparency = Blue Background?
- References:
- Bitmap + Clipboard + Transparency = Blue Background?
- From: mcse3010
- Re: Bitmap + Clipboard + Transparency = Blue Background?
- From: Bob Powell [MVP]
- Re: Bitmap + Clipboard + Transparency = Blue Background?
- From: mcse3010
- Re: Bitmap + Clipboard + Transparency = Blue Background?
- From: Michael Phillips, Jr.
- Re: Bitmap + Clipboard + Transparency = Blue Background?
- From: mcse3010
- Re: Bitmap + Clipboard + Transparency = Blue Background?
- From: mcse3010
- Re: Bitmap + Clipboard + Transparency = Blue Background?
- From: Michael Phillips, Jr.
- Re: Bitmap + Clipboard + Transparency = Blue Background?
- From: mcse3010
- Re: Bitmap + Clipboard + Transparency = Blue Background?
- From: Christopher Ireland
- Re: Bitmap + Clipboard + Transparency = Blue Background?
- From: Michael Phillips, Jr.
- Re: Bitmap + Clipboard + Transparency = Blue Background?
- From: Christopher Ireland
- Re: Bitmap + Clipboard + Transparency = Blue Background?
- From: Michael Phillips, Jr.
- Re: Bitmap + Clipboard + Transparency = Blue Background?
- From: Christopher Ireland
- Re: Bitmap + Clipboard + Transparency = Blue Background?
- From: Michael Phillips, Jr.
- Re: Bitmap + Clipboard + Transparency = Blue Background?
- From: Christopher Ireland
- Re: Bitmap + Clipboard + Transparency = Blue Background?
- From: Michael Phillips, Jr.
- Re: Bitmap + Clipboard + Transparency = Blue Background?
- From: Christopher Ireland
- Re: Bitmap + Clipboard + Transparency = Blue Background?
- From: Michael Phillips, Jr.
- Re: Bitmap + Clipboard + Transparency = Blue Background?
- From: Christopher Ireland
- Re: Bitmap + Clipboard + Transparency = Blue Background?
- From: Michael Phillips, Jr.
- Bitmap + Clipboard + Transparency = Blue Background?
- Prev by Date: GC.Collect() not cleaning memory, how to find out what references to lots of memory still exist?
- Next by Date: Re: Bitmap + Clipboard + Transparency = Blue Background?
- Previous by thread: Re: Bitmap + Clipboard + Transparency = Blue Background?
- Next by thread: Re: Bitmap + Clipboard + Transparency = Blue Background?
- Index(es):
Relevant Pages
|