如何将图像直接从相机或智能手机拖到 C# 应用程序?

How To drag images directly from Camera or Smartphone to C# app?

我找到了一个脚本 (http://www.codeproject.com/Articles/28209/Outlook-Drag-and-Drop-in-C),可以将图像拖到 C# 应用程序中。 (尤其是来自 outlook)fileDrop 格式用于将图像从我的硬盘复制(拖动)到 c# 应用程序。 当图像存储在我的硬盘上时,这很有效,但是当我尝试直接从我的存储卡(从相机或智能手机(如三星 S3))拖动图像时,它不起作用。 这些是我从这些图像中获得的拖动格式:

(0): "Shell IDList Array"
(1): "FileContents"
(2): "FileGroupDescriptorW"
(3): "WPD Storage Attributes"
(4): "Preferred DropEffect"
(5): "WPD NSE"
(6): "WPD NSE PnPDevicePath"
(7): "WPD NSE StoragePUID"
(8): "UsingDefaultDragImage"
(9): "DragImageBits"
(10): "DragContext"
(11): "DragSourceHelperFlags"
(12): "InShellDragLoop"
(13): "IsShowingLayered"
(14): "DragWindow"
(15): "IsComputingImage"
(16): "DataObjectAttributes"
(17): "DisableDragText"
(18): "IsShowingText"
(19): "DropDescription"
(20): "ComputedDragImage"
(21): "Logical Performed DropEffect"
(22): "Performed DropEffect"
(23): "Paste Succeeded"

当我尝试访问 'FileGroupDescriptorW' 时,我收到非法访问冲突错误。还有,'FileGroupDescriptor'这里好像不见了? 谁能帮我解决这个问题?我搜索了这个网站和 Google,但没有找到任何有用的东西。

解决方案由 John Schroedl 发布,隐藏在主题的许多反应中。

这两个 'fixes' 解决了我的问题:

http://www.codeproject.com/Articles/28209/Outlook-Drag-and-Drop-in-C?msg=3535951#xx3535951xx

旧 C#:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public sealed class FILEGROUPDESCRIPTORA
{
    public uint cItems;
    public FILEDESCRIPTORA[] fgd;
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public sealed class FILEGROUPDESCRIPTORW
{
    public uint cItems;
    public FILEDESCRIPTORW[] fgd;
}

已修复 C#:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public sealed class FILEGROUPDESCRIPTORA
{
    public uint cItems;
    public FILEDESCRIPTORA fgd;
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public sealed class FILEGROUPDESCRIPTORW
{
    public uint cItems;
    public FILEDESCRIPTORW fgd;
}

此修复:http://www.codeproject.com/Articles/28209/Outlook-Drag-and-Drop-in-C?msg=3551197#xx3551197xx

旧:

case "FileContents":
    //override the default handling of FileContents which returns the
    //contents of the first file as a memory stream and instead return
    //a array of MemoryStreams containing the data to each file dropped

    //get the array of filenames which lets us know how many file contents exist
    string[] fileContentNames = (string[])this.GetData("FileGroupDescriptor");

修复:

case "FileContents":
    //override the default handling of FileContents which returns the
    //contents of the first file as a memory stream and instead return
    //a array of MemoryStreams containing the data to each file dropped
    //
    // FILECONTENTS requires a companion FILEGROUPDESCRIPTOR to be
    // available so we bail out if we don't find one in the data object.

    string fgdFormatName;

    if (GetDataPresent("FileGroupDescriptorW"))
        fgdFormatName = "FileGroupDescriptorW";
    else if (GetDataPresent("FileGroupDescriptor"))
        fgdFormatName = "FileGroupDescriptor";
    else
        return null;

    //get the array of filenames which lets us know how many file contents exist
    string[] fileContentNames = (string[])this.GetData(fgdFormatName);

以防万一有人需要...