如何在Shell文件扩展名中添加更多命令? [英] How can i add more commands to the shell file extensions?

查看:84
本文介绍了如何在Shell文件扩展名中添加更多命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的是添加两个上下文菜单,复制到灰度和列出所有图像。



我传递了一个参数 List / 复制以区分它们。

  string menuCommand = string.Format( \ {0} \ {1 } \ \%L\ CopyGrayscaleImage, List,Application.ExecutablePath); 

但这是错误的。



menuCommand命令行应该是什么样子以及如何在Main方法中处理它?<​​/ p>

这是类FileShellExtensions

 使用System; 
使用System.Diagnostics;
使用Microsoft.Win32;

名称空间SimpleContextMenu
{
///< summary>
///注册和取消注册简单的shell上下文菜单。 ///< / summary>
静态类FileShellExtension
{
///< summary>
///注册一个简单的shell上下文菜单
/ //< / summary>
///< param < / param> name = fileType>要注册的文件类型。
///< param name = shellKeyName>出现在注册表中的名称。< / param>
///< param name = menuText>出现在上下文菜单中的文本。
///< param name = menuCommand>执行的命令行。< / param>
公共静态无效寄存器(
字符串fileType,字符串shellKeyName,
字符串menuText,字符串menuCommand)
{
Debug.Assert(!string.IsNullOrEmpty(fileType)& ;&
!string.IsNullOrEmpty(shellKeyName)&&
!string.IsNullOrEmpty(menuText)&&
!string.IsNullOrEmpty(menuCommand));

//创建注册表位置的完整路径
字符串regPath = string.Format(@ {0} \shell\ {1},fileType,shellKeyName);

//使用(RegistryKey key = Registry.ClassesRoot.CreateSubKey(regPath))将上下文菜单添加到注册表

{
key.SetValue(null,menuText );
}

//添加使用(RegistryKey键= Registry.ClassesRoot.CreateSubKey(
string.Format(@ {0 } \command,regPath)))
{
key.SetValue(null,menuCommand);
}
}

///< summary>
///取消注册简单的shell上下文菜单。
///< / summary>
///< param name = fileType>要注销的文件类型。< / param>
///< param name = shellKeyName>在注册表中注册的名称。< / param>
公共静态无效取消注册(字符串fileType,字符串shellKeyName)
{
Debug.Assert(!string.IsNullOrEmpty(fileType)&&
!string.IsNullOrEmpty(shellKeyName) );

//注册表位置的完整路径
字符串regPath = string.Format(@ {0} \shell\ {1},fileType,shellKeyName);

//从注册表中删除上下文菜单
Registry.ClassesRoot.DeleteSubKeyTree(regPath);
}
}

}

这是Progrsm.cs

 使用系统; 
使用System.Windows.Forms;
使用System.IO;
使用System.Drawing;
使用System.Drawing.Imaging;

[汇编:CLSCompliant(true)]
名称空间SimpleContextMenu
{
静态类Program
{
//要注册的文件类型
const string FileType = jpegfile;

//注册表中的上下文菜单名称
const string KeyName = Simple Context Menu;
const string KeyName1 =简单上下文菜单1;

//上下文菜单文本
const string MenuText =复制到灰度
const string MenuText1 =列出所有图像;

[STAThread]
静态void Main(string [] args)
{
//进程注册或注销命令
if(!ProcessCommand(args ))
{
//从shell调用,处理所选文件
CopyGrayscaleImage(args [0]);
}
}

///< summary>
///处理命令行操作(注册或注销)。
///< / summary>
///< param name = args>命令行参数。< / param>
///< returns>如果在命令行中处理了操作,则为真。
static bool ProcessCommand(string [] args)
{
//注册
if(args.Length == 0 || string.Compare(args [0],- register,true)== 0)
{
//自身的完整路径,%L是所选文件的占位符
// string menuCommand = string.Format(
/ /\"\\"{0}\\"\\"%L\\"\",Application.ExecutablePath);
字符串menuCommand = string.Format( \ {0} \ {1} \ \%L\ CopyGrayscaleImage, List,Application.ExecutablePath);

//注册上下文菜单
FileShellExtension.Register(Program.FileType,
Program.KeyName,Program.MenuText,
menuCommand);

FileShellExtension.Register(Program.FileType,
Program.KeyName1,Program.MenuText1,
menuCommand);

MessageBox.Show(string.Format(
0}已注册shell扩展名。,
Program.KeyName),Program.KeyName);

返回true;
}

//取消注册
if(string.Compare(args [0], -unregister,true)== 0)
{
//注销上下文菜单
FileShell Extension.Unregister(Program.FileType,Program.KeyName);

MessageBox.Show(string.Format(
{0} shell扩展未注册。,
Program.KeyName),Program.KeyName);

返回true;
}

//命令行未包含操作
return false;
}

///< summary>
///制作图像的灰度副本。
///< / summary>
///< param name = filePath>要复制的图像的完整路径。
static void CopyGrayscaleImage(string filePath)
{
try
{
//灰度副本的完整路径
字符串grayFilePath = Path.Combine(
Path.GetDirectoryName(filePath),
string.Format( {0}(灰度){1},
Path.GetFileNameWithoutExtension(filePath),
Path.GetExtension(filePath) )));

//使用调用在对象上处理,重要的
//因此,当应用终止
时,使用(Image image = new Bitmap(filePath))$不会锁定文件b $ b使用(位图grayImage =新的Bitmap(image.Width,image.Height))
使用(Graphics g = Graphics.FromImage(grayImage))
{
//设置灰度矩阵
ImageAttributes attr = new ImageAttributes();
attr.SetColorMatrix(new ColorMatrix(new float [] [] {
new float [] {0.3086F,0.3086F,0.3086F,0,0},
new float [] { 0.6094F,0.6094F,0.6094F,0,0},
新的float [] {0.082F,0.082F,0.082F,0,0},
新的float [] {0,0, 0,1,0,0},
个新的float [] {0,0,0,0,1,0},
个新的float [] {0,0,0,0,0, 1}}));

//创建灰度图像
g.DrawImage(image,new Rectangle(0,0,image.Width,image.Height),
0,0,image。宽度image.Height GraphicsUnit.Pixel attr);

//保存到文件系统
grayImage.Save(grayFilePath,ImageFormat.Jpeg);

//成功
MessageBox.Show(string.Format(复制的灰度图像{0},grayFilePath),Program.KeyName);
}
}
catch(异常例外)
{
MessageBox.Show(string.Format(发生错误:{0},例如Message。) ,Program.KeyName);
的回报;
}
}
}
}

更新



现在是Program.cs

 正在使用系统; 
使用System.Windows.Forms;
使用System.IO;
使用System.Drawing;
使用System.Drawing.Imaging;

[汇编:CLSCompliant(true)]
名称空间SimpleContextMenu
{
静态类Program
{
//要注册的文件类型
const string FileType = bitmapfile; // jpegfile;

//注册表中的上下文菜单名称
const string KeyName = Simple Context Menu;
const string KeyName1 =简单上下文菜单1;

//上下文菜单文本
const string MenuText =复制到灰度
const string MenuText1 =调整所有图像的大小;

[STAThread]
静态void Main(string [] args)
{
System.Threading.Thread.Sleep(30000);
//处理注册或注销命令
if(!ProcessCommand(args))
{
string action = args [0];
MessageBox.Show(action);
字符串fileName = args [1];

if(action == Copy)
{
//从shell调用,处理所选文件
CopyGrayscaleImage(fileName);
}
else if(动作==调整大小)
{
字符串FilePath = Path.Combine(
Path.GetDirectoryName(fileName),
string.Format( {0}(已调整大小){1},
Path.GetFileNameWithoutExtension(fileName),
Path.GetExtension(fileName))));
MessageBox.Show(FilePath);
位图bmp1 =新位图(ResizeImages(FilePath,100,100));
bmp1.Save(FilePath);
bmp1.Dispose();
}
}
}

///< summary>
///处理命令行操作(注册或注销)。
///< / summary>
///< param name = args>命令行参数。< / param>
///< returns>如果在命令行中处理了操作,则为真。
static bool ProcessCommand(string [] args)
{
//注册
if(args.Length == 0 || string.Compare(args [0],- register,true)== 0)
{
//自身的完整路径,%L是所选文件的占位符
字符串menuCommand = string.Format( \ {0} Copy复制\%L\,Application.ExecutablePath);

//注册上下文菜单
FileShellExtension.Register(Program.FileType,
Program.KeyName,Program.MenuText,
menuCommand);

字符串menuCommand1 = string.Format( \ {0} \调整大小\%L\,Application.ExecutablePath);

FileShellExtension.Register(Program.FileType,
Program.KeyName1,Program.MenuText1,
menuCommand1);

MessageBox.Show(string.Format(
{0} shell扩展已注册。,
Program.KeyName),Program.KeyName);

返回true;
}

//注销
if(string.Compare(args [0], -unregister,true)== 0)
{
//取消注册上下文菜单
FileShellExtension.Unregister(Program.FileType,Program.KeyName);

MessageBox.Show(string.Format(
{0} shell扩展未注册。,
Program.KeyName),Program.KeyName);

返回true;
}

//命令行未包含操作
return false;
}

///< summary>
///制作图像的灰度副本。
///< / summary>
///< param name = filePath>要复制的图像的完整路径。
static void CopyGrayscaleImage(string filePath)
{
try
{
//灰度副本的完整路径
字符串grayFilePath = Path.Combine(
Path.GetDirectoryName(filePath),
string.Format( {0}(灰度){1},
Path.GetFileNameWithoutExtension(filePath),
Path.GetExtension(filePath) )));

//使用调用在对象上处理,重要的
//因此,当应用终止
时,使用(Image image = new Bitmap(filePath))$不会锁定文件b $ b使用(位图grayImage =新的Bitmap(image.Width,image.Height))
使用(Graphics g = Graphics.FromImage(grayImage))
{
//设置灰度矩阵
ImageAttributes attr = new ImageAttributes();
attr.SetColorMatrix(new ColorMatrix(new float [] [] {
new float [] {0.3086F,0.3086F,0.3086F,0,0},
new float [] { 0.6094F,0.6094F,0.6094F,0,0},
新的float [] {0.082F,0.082F,0.082F,0,0},
新的float [] {0,0, 0,1,0,0},
新的float [] {0,0,0,0,1,0},
新的float [] {0,0,0,0,0, 1}}));

//创建灰度图像
g.DrawImage(image,new Rectangle(0,0,image.Width,image.Height),
0,0,image。宽度image.Height GraphicsUnit.Pixel attr);

//保存到文件系统
grayImage.Save(grayFilePath,ImageFormat.Jpeg);

//成功
MessageBox.Show(string.Format(复制的灰度图像{0},grayFilePath),Program.KeyName);
}
}
catch(异常例外)
{
MessageBox.Show(string.Format(发生错误:{0},例如Message。) ,Program.KeyName);
的回报;
}
}

私有静态位图ResizeImages(String filename,int maxWidth,int maxHeight)
{
using(Image originalImage = Image.FromFile( filename))
{
//计算新大小
int newWidth = originalImage.Width;
int newHeight = originalImage.Height;
double AspectRatio =(double)originalImage.Width /(double)originalImage.Height;
if(aspectRatio< = 1&& originalImage.Width> maxWidth)
{
newWidth = maxWidth;
newHeight =(int)Math.Round(newWidth / AspectRatio);
}
else if(aspectRatio> 1&& originalImage.Height> maxHeight)
{
newHeight = maxHeight;
newWidth =(int)Math.Round(newHeight * AspectRatio);
}
if(newWidth> = 0&& newHeight> = 0)
{
位图newImage =新位图(newWidth,newHeight);
使用(图形g = Graphics.FromImage(newImage))
{
//-质量设置调整以适合您的应用程序
g.InterpolationMode = System.Drawing.Drawing2D。 InterpolationMode.HighQualityBilinear;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.DrawImage(originalImage,0,0,newImage.Width,newImage.Height);
返回newImage;
}
}
返回null;
}
}
}
}

由于某些原因,调整大小部分不起作用。
我无法调试它,但是它应该在我的硬盘上保存一个调整大小的位图图像文件,但不是。

解决方案

看看 menuCommand 字符串,第一部分应该是程序的路径: Application.ExecutablePath ,第二部分可以是您指定的操作(复制或列表),第三部分可以是部分可以是所选项目的完整路径,由%L占位符表示。



所以正确的方法是

  string menuCommand = string.Format( \ {0} \复制\%L\,Application.ExecutablePath); 

//注册上下文菜单
FileShellExtension.Register(Program.FileType,
Program.KeyName,Program.MenuText,
menuCommand);

字符串menuCommand1 = string.Format( \ {0} \列表\%L\,Application.ExecutablePath);

//注册上下文菜单1
FileShellExtension.Register(Program.FileType,
Program.KeyName1,Program.MenuText1,
menuCommand1);

Main方法

  static void Main(string [] args)
{
//进程注册或注销命令
if(!ProcessCommand(args))
{
字符串操作= args [0];
MessageBox.Show(action);
字符串fileName = args [1];

if(action == Copy)
{
//从shell调用,处理所选文件
CopyGrayscaleImage(fileName);
}
else if(action == List)
{
ListImage(fileName);
}
}
}


What I am trying to do is adding two context menus, "Copy to Grayscale" and "List all images".

I pass a parameter "List"/"Copy" to distinguish them.

string menuCommand = string.Format("\"{0}\"{1}\" \"%L\" CopyGrayscaleImage", " List", Application.ExecutablePath);

But this is wrong.

What the menuCommand line should look like and how to handle it in the Main method ?

This is the class FileShellExtensions

using System;
using System.Diagnostics;
using Microsoft.Win32;

namespace SimpleContextMenu
{
    /// <summary>
    /// Register and unregister simple shell context menus.
    /// </summary>
    static class FileShellExtension
    {
        /// <summary>
        /// Register a simple shell context menu.
        /// </summary>
        /// <param name="fileType">The file type to register.</param>
        /// <param name="shellKeyName">Name that appears in the registry.</param>
        /// <param name="menuText">Text that appears in the context menu.</param>
        /// <param name="menuCommand">Command line that is executed.</param>
        public static void Register(
            string fileType, string shellKeyName, 
            string menuText, string menuCommand)
        {
            Debug.Assert(!string.IsNullOrEmpty(fileType) &&
                !string.IsNullOrEmpty(shellKeyName) &&
                !string.IsNullOrEmpty(menuText) && 
                !string.IsNullOrEmpty(menuCommand));

            // create full path to registry location
            string regPath = string.Format(@"{0}\shell\{1}", fileType, shellKeyName);

            // add context menu to the registry
            using (RegistryKey key = Registry.ClassesRoot.CreateSubKey(regPath))
            {
                key.SetValue(null, menuText);
            }

            // add command that is invoked to the registry
            using (RegistryKey key = Registry.ClassesRoot.CreateSubKey(
                string.Format(@"{0}\command", regPath)))
            {               
                key.SetValue(null, menuCommand);
            }
        }

        /// <summary>
        /// Unregister a simple shell context menu.
        /// </summary>
        /// <param name="fileType">The file type to unregister.</param>
        /// <param name="shellKeyName">Name that was registered in the registry.</param>
        public static void Unregister(string fileType, string shellKeyName)
        {
            Debug.Assert(!string.IsNullOrEmpty(fileType) &&
                !string.IsNullOrEmpty(shellKeyName));

            // full path to the registry location           
            string regPath = string.Format(@"{0}\shell\{1}", fileType, shellKeyName);

            // remove context menu from the registry
            Registry.ClassesRoot.DeleteSubKeyTree(regPath);
        }
    }

}

This is the Progrsm.cs

using System;
using System.Windows.Forms;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;

[assembly: CLSCompliant(true)]
namespace SimpleContextMenu
{
    static class Program
    {
        // file type to register
        const string FileType = "jpegfile";

        // context menu name in the registry
        const string KeyName = "Simple Context Menu";
        const string KeyName1 = "Simple Context Menu1";

        // context menu text
        const string MenuText = "Copy to Grayscale";
        const string MenuText1 = "List all images";

        [STAThread]
        static void Main(string[] args)
        {
            // process register or unregister commands
            if (!ProcessCommand(args))
            {
                // invoked from shell, process the selected file
                CopyGrayscaleImage(args[0]);
            }
        }

        /// <summary>
        /// Process command line actions (register or unregister).
        /// </summary>
        /// <param name="args">Command line arguments.</param>
        /// <returns>True if processed an action in the command line.</returns>
        static bool ProcessCommand(string[] args)
        {
            // register
            if (args.Length == 0 || string.Compare(args[0], "-register", true) == 0)
            {
                // full path to self, %L is placeholder for selected file
                //string menuCommand = string.Format(
                    //"\"{0}\" \"%L\"", Application.ExecutablePath);
                string menuCommand = string.Format("\"{0}\"{1}\" \"%L\" CopyGrayscaleImage", " List", Application.ExecutablePath);

                // register the context menu
                FileShellExtension.Register(Program.FileType,
                    Program.KeyName, Program.MenuText,
                    menuCommand);

                FileShellExtension.Register(Program.FileType,
                    Program.KeyName1, Program.MenuText1,
                    menuCommand);

                MessageBox.Show(string.Format(
                    "The {0} shell extension was registered.",
                    Program.KeyName), Program.KeyName);

                return true;
            }

            // unregister       
            if (string.Compare(args[0], "-unregister", true) == 0)
            {
                // unregister the context menu
                FileShellExtension.Unregister(Program.FileType, Program.KeyName);

                MessageBox.Show(string.Format(
                    "The {0} shell extension was unregistered.",
                    Program.KeyName), Program.KeyName);

                return true;
            }

            // command line did not contain an action
            return false;
        }

        /// <summary>
        /// Make a grayscale copy of the image.
        /// </summary>
        /// <param name="filePath">Full path to the image to copy.</param>
        static void CopyGrayscaleImage(string filePath)
        {
            try
            {
                // full path to the grayscale copy
                string grayFilePath = Path.Combine(
                    Path.GetDirectoryName(filePath),
                    string.Format("{0} (grayscale){1}",
                    Path.GetFileNameWithoutExtension(filePath),
                    Path.GetExtension(filePath)));

                // using calls Dispose on the objects, important 
                // so the file is not locked when the app terminates
                using (Image image = new Bitmap(filePath))
                using (Bitmap grayImage = new Bitmap(image.Width, image.Height))
                using (Graphics g = Graphics.FromImage(grayImage))
                {
                    // setup grayscale matrix
                    ImageAttributes attr = new ImageAttributes();
                    attr.SetColorMatrix(new ColorMatrix(new float[][]{   
                        new float[]{0.3086F,0.3086F,0.3086F,0,0},
                        new float[]{0.6094F,0.6094F,0.6094F,0,0},
                        new float[]{0.082F,0.082F,0.082F,0,0},
                        new float[]{0,0,0,1,0,0},
                        new float[]{0,0,0,0,1,0},
                        new float[]{0,0,0,0,0,1}}));

                    // create the grayscale image
                    g.DrawImage(image, new Rectangle(0, 0, image.Width, image.Height),
                        0, 0, image.Width, image.Height, GraphicsUnit.Pixel, attr);

                    // save to the file system
                    grayImage.Save(grayFilePath, ImageFormat.Jpeg);

                    // success
                    MessageBox.Show(string.Format("Copied grayscale image {0}", grayFilePath), Program.KeyName);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("An error occurred: {0}", ex.Message), Program.KeyName);
                return;
            }
        }
    }
}

UPDATE

This is Program.cs now

using System;
using System.Windows.Forms;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;

[assembly: CLSCompliant(true)]
namespace SimpleContextMenu
{
    static class Program
    {
        // file type to register
        const string FileType = "bitmapfile";//"jpegfile";

        // context menu name in the registry
        const string KeyName = "Simple Context Menu";
        const string KeyName1 = "Simple Context Menu1";

        // context menu text
        const string MenuText = "Copy to Grayscale";
        const string MenuText1 = "Resize all images";

        [STAThread]
        static void Main(string[] args)
        {
            System.Threading.Thread.Sleep(30000);
            // process register or unregister commands
            if (!ProcessCommand(args))
            {
                string action = args[0];
                MessageBox.Show(action);
                string fileName = args[1];

                if (action == "Copy")
                {
                    // invoked from shell, process the selected file
                    CopyGrayscaleImage(fileName);
                }
                else if (action == "Resize")
                {
                    string FilePath = Path.Combine(
                    Path.GetDirectoryName(fileName),
                    string.Format("{0} (resized){1}",
                    Path.GetFileNameWithoutExtension(fileName),
                    Path.GetExtension(fileName)));
                    MessageBox.Show(FilePath);
                    Bitmap bmp1 = new Bitmap(ResizeImages(FilePath,100,100));
                    bmp1.Save(FilePath);
                    bmp1.Dispose();
                }
            }
        }

        /// <summary>
        /// Process command line actions (register or unregister).
        /// </summary>
        /// <param name="args">Command line arguments.</param>
        /// <returns>True if processed an action in the command line.</returns>
        static bool ProcessCommand(string[] args)
        {
            // register
            if (args.Length == 0 || string.Compare(args[0], "-register", true) == 0)
            {
                // full path to self, %L is placeholder for selected file
                string menuCommand = string.Format("\"{0}\" Copy \"%L\"", Application.ExecutablePath);

                // register the context menu
                FileShellExtension.Register(Program.FileType,
                    Program.KeyName, Program.MenuText,
                    menuCommand);

                string menuCommand1 = string.Format("\"{0}\" Resize \"%L\"", Application.ExecutablePath);

                FileShellExtension.Register(Program.FileType,
                    Program.KeyName1, Program.MenuText1,
                    menuCommand1);

                MessageBox.Show(string.Format(
                    "The {0} shell extension was registered.",
                    Program.KeyName), Program.KeyName);

                return true;
            }

            // unregister       
            if (string.Compare(args[0], "-unregister", true) == 0)
            {
                // unregister the context menu
                FileShellExtension.Unregister(Program.FileType, Program.KeyName);

                MessageBox.Show(string.Format(
                    "The {0} shell extension was unregistered.",
                    Program.KeyName), Program.KeyName);

                return true;
            }

            // command line did not contain an action
            return false;
        }

        /// <summary>
        /// Make a grayscale copy of the image.
        /// </summary>
        /// <param name="filePath">Full path to the image to copy.</param>
        static void CopyGrayscaleImage(string filePath)
        {
            try
            {
                // full path to the grayscale copy
                string grayFilePath = Path.Combine(
                    Path.GetDirectoryName(filePath),
                    string.Format("{0} (grayscale){1}",
                    Path.GetFileNameWithoutExtension(filePath),
                    Path.GetExtension(filePath)));

                // using calls Dispose on the objects, important 
                // so the file is not locked when the app terminates
                using (Image image = new Bitmap(filePath))
                using (Bitmap grayImage = new Bitmap(image.Width, image.Height))
                using (Graphics g = Graphics.FromImage(grayImage))
                {
                    // setup grayscale matrix
                    ImageAttributes attr = new ImageAttributes();
                    attr.SetColorMatrix(new ColorMatrix(new float[][]{   
                        new float[]{0.3086F,0.3086F,0.3086F,0,0},
                        new float[]{0.6094F,0.6094F,0.6094F,0,0},
                        new float[]{0.082F,0.082F,0.082F,0,0},
                        new float[]{0,0,0,1,0,0},
                        new float[]{0,0,0,0,1,0},
                        new float[]{0,0,0,0,0,1}}));

                    // create the grayscale image
                    g.DrawImage(image, new Rectangle(0, 0, image.Width, image.Height),
                        0, 0, image.Width, image.Height, GraphicsUnit.Pixel, attr);

                    // save to the file system
                    grayImage.Save(grayFilePath, ImageFormat.Jpeg);

                    // success
                    MessageBox.Show(string.Format("Copied grayscale image {0}", grayFilePath), Program.KeyName);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("An error occurred: {0}", ex.Message), Program.KeyName);
                return;
            }
        }

        private static Bitmap ResizeImages(String filename, int maxWidth, int maxHeight)
        {
            using (Image originalImage = Image.FromFile(filename))
            {
                //Caluate new Size
                int newWidth = originalImage.Width;
                int newHeight = originalImage.Height;
                double aspectRatio = (double)originalImage.Width / (double)originalImage.Height;
                if (aspectRatio <= 1 && originalImage.Width > maxWidth)
                {
                    newWidth = maxWidth;
                    newHeight = (int)Math.Round(newWidth / aspectRatio);
                }
                else if (aspectRatio > 1 && originalImage.Height > maxHeight)
                {
                    newHeight = maxHeight;
                    newWidth = (int)Math.Round(newHeight * aspectRatio);
                }
                if (newWidth >= 0 && newHeight >= 0)
                {
                    Bitmap newImage = new Bitmap(newWidth, newHeight);
                    using (Graphics g = Graphics.FromImage(newImage))
                    {
                        //--Quality Settings Adjust to fit your application
                        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
                        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                        g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
                        g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                        g.DrawImage(originalImage, 0, 0, newImage.Width, newImage.Height);
                        return newImage;
                    }
                }
                return null;
            }
        }
    }
}

For some reason the Resize part is not owrking. I can't debug it but it should save a resized Bitmap image file on my hard disk but it's not. Can't figure out.

解决方案

Look at the menuCommand string, the first part should be the path to your program: Application.ExecutablePath, the second part can be the action you specified ("Copy" or "List"), and the third part can be the full path to the selected item, which is represented by the "%L" placeholder.

So the correct way is

string menuCommand = string.Format("\"{0}\" Copy \"%L\"", Application.ExecutablePath);

// register the context menu
FileShellExtension.Register(Program.FileType,
                Program.KeyName, Program.MenuText,
                menuCommand);

string menuCommand1 = string.Format("\"{0}\" List \"%L\"", Application.ExecutablePath);

// register the context menu 1
FileShellExtension.Register(Program.FileType,
                Program.KeyName1, Program.MenuText1,
                menuCommand1);

And the Main method

static void Main(string[] args)
{
    // process register or unregister commands
    if (!ProcessCommand(args))
    {
        string action = args[0];
        MessageBox.Show(action);
        string fileName = args[1];

        if (action == "Copy")
        {
            // invoked from shell, process the selected file
            CopyGrayscaleImage(fileName);
        }
        else if (action == "List")
        {
            ListImage(fileName); 
        }
    }
}

这篇关于如何在Shell文件扩展名中添加更多命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆