简体   繁体   English

C#错误CS0103当前上下文中不存在名称“ originalImage”

[英]C# Error CS0103 The name 'originalImage' does not exist in the current context

I have problem with my code, I've tryed string originalImage = null; 我的代码有问题,我尝试了string originalImage = null; . But this not really working. 但这不是真的。 Because its not taking original file name somehow.. 因为它不采用原始文件名。

Code: 码:

 private void textBox1_Click(object sender, EventArgs e)
    {
        FolderBrowserDialog fbd = new FolderBrowserDialog();
        fbd.RootFolder = Environment.SpecialFolder.Desktop;
        fbd.Description = "+++ Select path +++";
        fbd.ShowNewFolderButton = false;

        if (fbd.ShowDialog() == DialogResult.OK)
        {
            textBox1.Text = fbd.SelectedPath;
        }
        string[] originalImage = Directory.GetFiles(textBox1.Text, "*.JPG");

        foreach (var filename in originalImage)
        {
            Bitmap bitmap = new Bitmap(filename);

            //DefaultCompressionJpeg(bitmap);

            VariousQuality(bitmap);
        }
    }

    string originalImage = null;

    public void VariousQuality(Image original)
    {

        ImageCodecInfo jpgEncoder = null;
        ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
        foreach (ImageCodecInfo codec in codecs)
        {
            if (codec.FormatID == ImageFormat.Jpeg.Guid)
            {
                jpgEncoder = codec;
                break;
            }
        }
        if (jpgEncoder != null)
        {

            Encoder encoder = Encoder.Quality;
            EncoderParameters encoderParameters = new EncoderParameters(1);

            for (long quality = 90; quality <= 90;)
            {
                EncoderParameter encoderParameter = new EncoderParameter(encoder, quality);
                encoderParameters.Param[0] = encoderParameter;

                string fileOut = Path.Combine(@"C:\Users\Kristen\Desktop\pilt2",  originalImage + ".jpeg");
                Debug.WriteLine(fileOut);
                FileStream ms = new FileStream(fileOut, FileMode.Create, FileAccess.Write);
                original.Save(ms, jpgEncoder, encoderParameters);
                ms.Flush();
                ms.Close();
            }
        }
    }

Kind regards, 亲切的问候,

As someone suggested in the comments (which they mysteriously deleted because they were definitely on to something), it seems like you're trying to refer to the original argument: 正如某人在评论中所建议的那样(它们被神秘删除了,因为它们肯定是存在于某物上), 似乎您正在尝试引用original参数:

public void VariousQuality(Image original)

Just rename either that arg to originalImage or rename the other in your code to original . 只需将arg重命名为originalImage或将代码中的另一个重命名为original

In you click eventhandler you have a local variable string [] originalImage which you initialize with all the filenames in some directory. 在单击事件处理程序时,您将使用一个本地变量string [] originalImage ,并使用某个目录中的所有文件名进行初始化。

On class level you have a field string originalImage which you initialize with null . 在类级别,您有一个字段string originalImage ,使用null初始化。

These two elements do have nothing to do with each other, they are completely unrelated. 这两个元素彼此无关,它们是完全无关的。

So in your compression method you use an originalImage to construct a filename. 因此,在压缩方法中,您使用了originalImage来构造文件名。 The only entity of this name known in this method is the string field of the class, which has a null value. 在此方法中,此名称唯一的实体是类的字符串字段,该字段的值为null

You should add a second parameter to your compression method where you pass the current filename to your method and remove the field from the class. 您应该在压缩方法中添加第二个参数,将当前文件名传递给方法,然后从类中删除该字段。

public void VariousQuality (Bitmap original, string filename) {
   ...

 string fileOut = Path.Combine(@"C:\Users\Kristen\Desktop\pilt2", filename + ".jpeg");

}

Call the method as follows 调用方法如下

foreach (var filename in originalImage) { 
    Bitmap bitmap = new Bitmap(filename);
 //DefaultCompressionJpeg(bitmap);
    string fn = Path.GetFileNameWithoutExtension(filename);
    VariousQuality(bitmap, fn); 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#错误CS0103:名称“ i”在当前上下文中不存在 - C# Error CS0103: The name 'i' does not exist in the current context C# 错误:CS0103 当前上下文中不存在名称“ ” - C# error : CS0103 The name ' ' does not exist in the current context 错误 CS0103:当前上下文中不存在名称“_context” - Error CS0103: The name '_context' does not exist in the current context 错误 CS0103:当前上下文中不存在名称“currentScreen”(CS0103) - Error CS0103: The name 'currentScreen' does not exist in the current context (CS0103) 错误CS0103:名称“ TimeSpan”在当前上下文(CS0103)(testingProgram)中不存在? - Error CS0103: The name 'TimeSpan' does not exist in the current context (CS0103) (testingProgram)? 错误CS0103当前上下文中不存在名称“图像” - Error CS0103 The name 'Image' does not exist in the current context Unity错误CS0103:当前上下文中不存在名称`&#39; - Unity error CS0103: The name `' does not exist in the current context 错误 CS0103: 当前上下文中不存在名称“ ” - error CS0103: The name ' ' does not exist in the current context 错误 CS0103:当前上下文中不存在名称“x” - error CS0103: The name 'x' does not exist in the current context 错误 CS0103:当前上下文中不存在名称“IsInRole” - ERROR CS0103: The name 'IsInRole' does not exist in the current context
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM