Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WPF - StartDragging populate DataObject with Image #2687

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 25 additions & 10 deletions CefSharp.Wpf/ChromiumWebBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
using CefSharp.Enums;
using CefSharp.Internals;
Expand Down Expand Up @@ -745,16 +747,29 @@ bool IRenderWebBrowser.StartDragging(IDragData dragData, DragOperationsMask allo
currentDragData = dragData.Clone();
currentDragData.ResetFileContents();

// TODO: The following code block *should* handle images, but GetFileContents is
// not yet implemented.
//if (dragData.IsFile)
//{
// var bmi = new BitmapImage();
// bmi.BeginInit();
// bmi.StreamSource = dragData.GetFileContents();
// bmi.EndInit();
// dataObject.SetImage(bmi);
//}
if (dragData.HasImage)
{
IImage dragImage = dragData.Image;
int width, height;
byte[] pixels = dragImage.GetAsBitmap(1f, ColorType.Rgba8888, AlphaType.PreMultiplied, out width, out height);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the scale factor be dynamic based on the display? I would have though so.

int stride = ((width * 32 + 31) & ~31) / 8;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like your using PixelFormats.Pbgra32 below, so it should be the correct format. Use

(PixelFormats.Pbgra32.BitsPerPixel / 8) * width to calculate stride

https://github.com/cefsharp/CefSharp/blob/cefsharp/71/CefSharp.Wpf/Rendering/InteropBitmapRenderHandler.cs#L31

var bitmap = BitmapSource.Create(width, height, 96.0, 96.0, PixelFormats.Pbgra32, null, pixels, stride);
bitmap.Freeze();
dataObject.SetImage(bitmap);
}
else
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be removed if you've added support for image.

{
// TODO: The following code block *should* handle images, but GetFileContents is
// not yet implemented.
//if (dragData.IsFile)
//{
// var bmi = new BitmapImage();
// bmi.BeginInit();
// bmi.StreamSource = dragData.GetFileContents();
// bmi.EndInit();
// dataObject.SetImage(bmi);
//}
}

UiThreadRunAsync(delegate
{
Expand Down