修改了一下,拖拽出来的文件为ico

功能建议 · 49 次浏览
Anlv 创建于 2025-02-07 12:48
private void listView1_ItemDrag(object sender, ItemDragEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        if (listView1.SelectedItems.Count <= 0)
        {
            return;
        }

        // Put selected files into a string array
        string[] files = new String[listView1.SelectedItems.Count];

        int i = 0;
        string temp = Path.GetTempPath();
        foreach (ListViewItem item in listView1.SelectedItems)
        {
            var path = item.SubItems[3].Text;
            var name = Path.Combine(temp, item.Text.ToString() + ".ico");

            // Get the selected resolution from the dropdown
            int resolution = GetSelectedResolution();

            // Extract the icon as a BitmapSource
            BitmapSource bitmapSource = c_icon_of_path.icon_of_path_large(path, GetBoxValue(), true);

            // Convert BitmapSource to Bitmap
            using (var memoryStream = new MemoryStream())
            {
                BitmapEncoder encoder = new PngBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(bitmapSource));
                encoder.Save(memoryStream);

                using (var bitmap = new Bitmap(memoryStream))
                {
                    // Resize the bitmap to the selected resolution
                    using (var resizedBitmap = new Bitmap(bitmap, new Size(resolution, resolution)))
                    {
                        // Save as .ico file with full color information
                        SaveBitmapAsIcon(resizedBitmap, name);
                    }
                }
            }

            files[i++] = name;
        }

        // Create a DataObject holding this array as a file drop
        DataObject data = new DataObject(DataFormats.FileDrop, files);

        // Also add the selection as text data
        data.SetData(DataFormats.StringFormat, files[0]);

        // Do DragDrop
        DoDragDrop(data, DragDropEffects.Copy);
    }
}

/// <summary>
/// Get the selected resolution based on the dropdown value.
/// </summary>
/// <returns>The resolution (e.g., 16, 32, 48, 256).</returns>
private int GetSelectedResolution()
{
    switch (comboBox1.SelectedValue.ToString())
    {
        case "Small":
            return 16; // Small icon: 16x16
        case "Large":
            return 32; // Large icon: 32x32
        case "ExtraLarge":
            return 48; // Extra large icon: 48x48
        case "Jumbo":
            return 256; // Jumbo icon: 256x256
        default:
            return 32; // Default to 32x32
    }
}

/// <summary>
/// Save a Bitmap as an .ico file with full color information.
/// </summary>
/// <param name="bitmap">The Bitmap to save.</param>
/// <param name="filePath">The output .ico file path.</param>
private void SaveBitmapAsIcon(Bitmap bitmap, string filePath)
{
    using (var fileStream = new FileStream(filePath, FileMode.Create))
    {
        // Write the ICO header
        using (var binaryWriter = new BinaryWriter(fileStream))
        {
            // ICONDIR structure
            binaryWriter.Write((short)0); // Reserved, must be 0
            binaryWriter.Write((short)1); // Image type, 1 = icon
            binaryWriter.Write((short)1); // Number of images

            // ICONDIRENTRY structure
            binaryWriter.Write((byte)bitmap.Width); // Width
            binaryWriter.Write((byte)bitmap.Height); // Height
            binaryWriter.Write((byte)0); // Number of colors in palette
            binaryWriter.Write((byte)0); // Reserved, must be 0
            binaryWriter.Write((short)1); // Color planes
            binaryWriter.Write((short)32); // Bits per pixel

            // Calculate image size
            using (var iconStream = new MemoryStream())
            {
                bitmap.Save(iconStream, ImageFormat.Png);
                var imageSize = (int)iconStream.Length;
                binaryWriter.Write(imageSize); // Size of the image data
                binaryWriter.Write(22); // Offset of the image data (22 bytes for header)

                // Write the image data
                binaryWriter.Write(iconStream.ToArray());
            }
        }
    }
}

回复内容
暂无回复
回复主贴