Dynamically create bmp/cur from Picture?

Anything other than problems and feature requests.

Moderators: Frank Hileman, Anne Szyjan

Dynamically create bmp/cur from Picture?

Postby mr24shoe » Mon May 23, 2005 7:33 pm

Hopfully a quick question: Given an instance of a Picture, is it possible to create a bmp file at run time?
(We're trying to implement custom cursors with the drag drop stuff...)

Thanks,
Mark
mr24shoe
 
Posts: 24
Joined: Sat Jan 29, 2005 2:28 pm

Postby Frank Hileman » Mon May 23, 2005 8:23 pm

Yes. Create a Bitmap the correct size. Call Graphics.FromImage to get a Graphics. Set the Translation and Scaling properties on the Picture to fit into the Bitmap. Call Picture.RenderTo to render to the Bitmap.

The ScalingTranslation constructor that takes a source and destination rectangle works well -- if the third parameter is true, the scaling will be equal, preserving the aspect ratio. To apply the ScalingTranslation to the Picture, set the Picture TransformationReference to the origin, reset the Transformation on the Picture, and call TransformBy, passing in the ScalingTranslation:
Code: Select all
picture1.TransformationReference = TransfomationReference.CreateAbsolute(Vector.Empty);
picture1.ResetTransformation;
picture1.TransformBy(scalingTranslation);
User avatar
Frank Hileman
Site Admin
 
Posts: 1392
Joined: Sun Jul 25, 2004 8:16 pm
Location: California

Postby mr24shoe » Tue May 24, 2005 4:30 am

sweet, thanks! I will give it a shot and let you know how it goes...
mr24shoe
 
Posts: 24
Joined: Sat Jan 29, 2005 2:28 pm

Postby Frank Hileman » Tue May 24, 2005 8:43 am

Note this is very similar to printing a Picture, if you have done that. We probably will add some code to do all the scaling for you at some point.
User avatar
Frank Hileman
Site Admin
 
Posts: 1392
Joined: Sun Jul 25, 2004 8:16 pm
Location: California

Postby mr24shoe » Wed Jun 01, 2005 5:59 pm

ok, the RenderTo is not available to us yet, as we're still on the Lite version. I played around with the custom icon stuff with some simple 16x16 icons in visual studio, and I don't think that's what we're wanting anyway.

What we're trying to accomplish is a "faded/transparent" version of the Picture currently being dragged...much the same way the icons on your desktop show up when you drag them around (the original stays put, but there's a "copy" under the mouse....)

Could you clue us in on what we need to do to accomplish this?
mr24shoe
 
Posts: 24
Joined: Sat Jan 29, 2005 2:28 pm

Postby Frank Hileman » Thu Jun 02, 2005 5:14 am

This is unfortunate. Once you have rendered to a bitmap, ColorMatrix can reduce opacity.
User avatar
Frank Hileman
Site Admin
 
Posts: 1392
Joined: Sun Jul 25, 2004 8:16 pm
Location: California

Postby mr24shoe » Mon Jun 06, 2005 5:46 pm

Frank Hileman wrote:ok, the RenderTo is not available to us yet, as we're still on the Lite version. I played around with the custom icon stuff with some simple 16x16 icons in visual studio, and I don't think that's what we're wanting anyway.

What we're trying to accomplish is a "faded/transparent" version of the Picture currently being dragged...much the same way the icons on your desktop show up when you drag them around (the original stays put, but there's a "copy" under the mouse....)

Could you clue us in on what we need to do to accomplish this?


Frank Hileman wrote:This is unfortunate. Once you have rendered to a bitmap, ColorMatrix can reduce opacity.


OK, got RenderTo working, but the custom cursors seem to have to be 16x16...is this true? just to reiterate:
What we're trying to accomplish is a "faded/transparent" version of the Picture currently being dragged...much the same way the icons on your desktop show up when you drag them around (the original stays put, but there's a "copy" under the mouse....)

I can envision something like this happening w/in a single containing canvas (picture creates a new version of itself under the mouse in OnMouseMove?)...but we've got a shortcoming when dragging to another containing canvas. We'll continue experimenting, and keep you posted on findings...

Thanks again,
Mark
mr24shoe
 
Posts: 24
Joined: Sat Jan 29, 2005 2:28 pm

Postby Frank Hileman » Mon Jun 06, 2005 6:17 pm

We use 32x32 cursors and they work fine. I don't see why larger ones would not work. Maybe try playing with the bitmap settings? Keep the bitmap underlying stream open, as well -- they don't like it when you close them.
User avatar
Frank Hileman
Site Admin
 
Posts: 1392
Joined: Sun Jul 25, 2004 8:16 pm
Location: California

Postby mr24shoe » Tue Jun 07, 2005 7:04 pm

Was able to successfully use a 96x96 .ico file that was part of VS as a custom cursor. Pretty cool.

We've also successfully created a bmp and saved it to the hard drive based on the currentDraggedPicture:

Code: Select all
int h = Convert.ToInt32(_currentDrag.Size.Height);
int w = Convert.ToInt32(_currentDrag.Size.Width);
Bitmap b = new Bitmap(w,h);
Graphics g = Graphics.FromImage(b);
ScalingTranslation st = new ScalingTranslation(_currentDrag.Bounds, new FRectangle(0,0,w,h));
Transformation oldTrans = _currentDrag.Transformation;
_currentDrag.TransformationReference = TransformationReference.CreateAbsolute(Vector.Empty);
_currentDrag.TransformBy(st);
if (_currentDrag.RenderTo(g))
{
   b.Save(@"c:\test.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
}
_currentDrag.Transformation = oldTrans;


We're now trying to set the cursor to this new bmp via (essentially):

Code: Select all
Cursor.Current = new Cursor(@"c:\test.bmp");


...but are getting the following exception:
The argument 'picture' must be a picture that can be used as a Cursor.\r\nParameter name: picture


We've played around w/ the different constructor parameters when creating the bitmap, for example:
Code: Select all
Bitmap b = new Bitmap(w,h,System.Drawing.Imaging.PixelFormat.Format16bppArgb555); //,System.Drawing.Imaging.PixelFormat.Format8bppIndexed);



RenderTo doesn't like anything "indexed", and the cursor just doesn't like anything we've tried thus far.

Could you provide some clues?

Thanks again,
Mark
mr24shoe
 
Posts: 24
Joined: Sat Jan 29, 2005 2:28 pm

Postby Frank Hileman » Wed Jun 08, 2005 7:20 am

First there is the creation of a translucent image. You will need two Bitmaps: one in which the Picture is rendered, and a second one to make it more transparent. Both of these need to be cleared with a transparent background.
Code: Select all
using Rect = System.Drawing.Rectangle;
...
// NOTE: add some extra pixels to width, height, to account for
// half stroke width, if you use strokes
Bitmap b1 = new Bitmap(w,h);
using (Graphics g1 = Graphics.FromImage(b1))
{
  g1.Clear(Color.Transparent);
  ... the RenderTo code goes here ...
}

// now make it more transparent
ImageAttributes ia = new ImageAttributes();
ColorMatrix cm = new ColorMatrix();
cm.Matrix33 = 0.5F;  //50% transparency
ia.SetColorMatrix(cm);
Bitmap b2 = new Bitmap(w,h);
using (Graphics g2 = Graphics.FromImage(b2))
{
   g2.Clear(Color.Transparent);
   g2.DrawImageUnscaled(b,
       new Rect(0, 0, b1.Width, b1.Height),
       0, 0, b1.Width, b1.Height, GraphicsUnit.Pixel, ia);
  }
}
// now b2 has the right data, you can dispose b1

Now how do you display it over the whole form? It looks like an alpha blended cursor is not an ordinary bitmap. If that is the case you may prefer to use a new window that is alpha blended instead dragged around.

Here is some code I found that does change a cursor image:
http://groups-beta.google.com/group/microsoft.public.dotnet.framework.windowsforms/browse_thread/thread/b6cebdf616b913bc/8c8b8d86f7412dba

Here is an article that speaks about the cursor format. According to this article, it is not an ordinary alpha blended bitmap:
http://support.microsoft.com/default.aspx?scid=kb;en-us;318876

If you want to leave the cursor as is, and drag around a translucent window, you can do that. We are currently investigating adding a Form class to VG.net that will automatically be alpha blended, based on a Picture you add to it, but in the mean time:
http://www.codeproject.com/cs/media/perpxalpha_sharp.asp
User avatar
Frank Hileman
Site Admin
 
Posts: 1392
Joined: Sun Jul 25, 2004 8:16 pm
Location: California

Next

Return to General

Who is online

Users browsing this forum: Google [Bot] and 0 guests

cron