Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# GDI+;中发生一般性错误;保存图像时_C#_Gdi_System.drawing.graphics - Fatal编程技术网

C# GDI+;中发生一般性错误;保存图像时

C# GDI+;中发生一般性错误;保存图像时,c#,gdi,system.drawing.graphics,C#,Gdi,System.drawing.graphics,我读了同一个问题的答案,做了要求我做的任何事情。我还为当前用户授予了文件夹的写入权限,如前一个答案中所述,但仍然出现此错误。那么,有谁能告诉我具体的答案,该怎么办? 这是我的密码 protected void Page_Load(object sender, EventArgs e) { //get all the files in a directory string[] files = Directory.GetFiles(@"D:\Naved\BasicDotNet

我读了同一个问题的答案,做了要求我做的任何事情。我还为当前用户授予了文件夹的写入权限,如前一个答案中所述,但仍然出现此错误。那么,有谁能告诉我具体的答案,该怎么办? 这是我的密码

    protected void Page_Load(object sender, EventArgs e)
{
    //get all the files in a directory
    string[] files = Directory.GetFiles(@"D:\Naved\BasicDotNet\Images");

    //combine them into one image
    System.Drawing.Bitmap stitchedImage = Combine(files);

    //save the new image
    stitchedImage.Save(@"D:\Naved\BasicDotNet\Images", System.Drawing.Imaging.ImageFormat.Jpeg);//Error:-A generic error occurred in GDI+

}
public static System.Drawing.Bitmap Combine(string[] files)
{
    //read all images into memory
    List<System.Drawing.Bitmap> images = new List<System.Drawing.Bitmap>();
    System.Drawing.Bitmap finalImage = null;

    try
    {
        int width = 0;
        int height = 0;

        foreach (string image in files)
        {
            //create a Bitmap from the file and add it to the list
            System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(image);

            //update the size of the final bitmap
            width += bitmap.Width;
            height = bitmap.Height > height ? bitmap.Height : height;

            images.Add(bitmap);
        }

        //create a bitmap to hold the combined image
        finalImage = new System.Drawing.Bitmap(width, height);

        //get a graphics object from the image so we can draw on it
        using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(finalImage))
        {
            //set background color
            g.Clear(System.Drawing.Color.Black);

            //go through each image and draw it on the final image
            int offset = 0;
            foreach (System.Drawing.Bitmap image in images)
            {
                g.DrawImage(image,
                  new System.Drawing.Rectangle(offset, 0, image.Width, image.Height));
                offset += image.Width;
            }
        }

        return finalImage;
    }
    catch (Exception ex)
    {
        if (finalImage != null)
            finalImage.Dispose();

        throw ex;
    }
    finally
    {
        //clean up memory
        foreach (System.Drawing.Bitmap image in images)
        {
            image.Dispose();
        }
    }
}
}
受保护的无效页面加载(对象发送方,事件参数e)
{
//获取目录中的所有文件
string[]files=Directory.GetFiles(@“D:\Naved\BasicDotNet\Images”);
//将它们组合成一个图像
System.Drawing.Dimage=组合(文件);
//保存新图像
stitchedImage.Save(@“D:\Naved\BasicDotNet\Images”,System.Drawing.Imaging.ImageFormat.Jpeg);//错误:-GDI中发生一般错误+
}
公共静态System.Drawing.Bitmap组合(字符串[]文件)
{
//将所有图像读入内存
列表图像=新列表();
System.Drawing.Bitmap finalImage=null;
尝试
{
整数宽度=0;
整数高度=0;
foreach(文件中的字符串图像)
{
//从文件创建位图并将其添加到列表中
System.Drawing.Bitmap Bitmap=新的System.Drawing.Bitmap(图像);
//更新最终位图的大小
宽度+=位图宽度;
高度=位图。高度>高度?位图。高度:高度;
添加(位图);
}
//创建位图以保存组合图像
finalImage=新系统.绘图.位图(宽度,高度);
//从图像中获取图形对象,以便我们可以在其上绘制
使用(System.Drawing.Graphics g=System.Drawing.Graphics.FromImage(最终图像))
{
//设置背景色
g、 清晰(系统、图纸、颜色、黑色);
//浏览每个图像并在最终图像上绘制
整数偏移=0;
foreach(图像中的System.Drawing.Bitmap图像)
{
g、 DrawImage(图像,
新系统。绘图。矩形(偏移量,0,图像。宽度,图像。高度);
偏移量+=图像宽度;
}
}
返回最终授权;
}
捕获(例外情况除外)
{
if(finalImage!=null)
finalImage.Dispose();
掷骰子;
}
最后
{
//清理内存
foreach(图像中的System.Drawing.Bitmap图像)
{
image.Dispose();
}
}
}
}
试试看

而不是

stitchedImage.Save(@"D:\Naved\BasicDotNet\Images", System.Drawing.Imaging.ImageFormat.Jpeg);
第一个参数是要保存的文件名,而不是要保存的目录。以前你做过

string[] files = Directory.GetFiles(@"D:\Naved\BasicDotNet\Images");
所以
@“D:\Naved\BasicDotNet\Images”
必须是目录,而不是文件


(顺便提一下,我建议将缝合的图像保存到其他目录;否则,每次运行代码时,您都会将上一个缝合与上一个内容缝合在一起。)

图像有多大?您正在从
@“D:\Naved\BasicDotNet\images”
加载图像,所以那一定是一个目录——但你确实是这样做的——试图用一个同名的文件覆盖先前存在的目录。请尝试其他名称。
Image.Save的
string
参数是要保存的文件,而不是要保存的目录。
string[] files = Directory.GetFiles(@"D:\Naved\BasicDotNet\Images");
stitchedImage.Save(@"D:\Naved\BasicDotNet\Images", System.Drawing.Imaging.ImageFormat.Jpeg); - you need to specify the file name here. I.e edit it to something like this:     `stitchedImage.Save(@"D:\Naved\BasicDotNet\Images\myjpeg.Jpeg", System.Drawing.Imaging.ImageFormat.Jpeg)`