sharpshell icon indicating copy to clipboard operation
sharpshell copied to clipboard

Generic GDI+ Error

Open natan-fernandes opened this issue 5 years ago • 3 comments
trafficstars

Version of SharpShell used: 2.7.2 Related type(s) of SharpShell-Server: Any

I'm trying to use the ThumbnailHandler and I keep getting this error when using the "Test Server in Test Shell" option. I've tried creating my own dll modifying ".natan" files and also tried the sample one that modifies ".txt" files. Is there something I'm missing? I've also tried returning a 1px x 1px bitmap but had no success

Log Messages

System.Runtime.InteropServices.ExternalException (0x80004005): A generic error occurred in GDI+. at System.Drawing.Image.FromHbitmap(IntPtr hbitmap, IntPtr hpalette) at System.Drawing.Image.FromHbitmap(IntPtr hbitmap) at ServerManager.TestShell.ShellThumbnailHost.SetPreviewItem(ShellItem shellItem) in C:\projects\sharpshell\SharpShell\Tools\ServerManager\TestShell\ShellThumbnailHost.cs:line 41 at ServerManager.TestShell.TestShellForm.shellListView_SelectedIndexChanged(Object sender, EventArgs e) in C:\projects\sharpshell\SharpShell\Tools\ServerManager\TestShell\TestShellForm.cs:line 244 at System.Windows.Forms.ListView.OnSelectedIndexChanged(EventArgs e) at System.Windows.Forms.ListView.WmReflectNotify(Message& m) at System.Windows.Forms.ListView.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

natan-fernandes avatar Oct 19 '20 13:10 natan-fernandes

After some research I found that something was wrong at ThumbnailHandler class. I'm using Visual Studio Community 2019 and the project was made on 2017 but I don't think it would interfere in something.

So, that's what I managed to fix the error:

Inside GetThumbnailImage(uint width) method I found at Code Project, there's a StreamReader adding the lines of the file to a list called previewLines. When disposing the StreamReader, it was skipping the return line (I don't know why) and then it entered the catch block returning null. I removed the StreamReader stuff and added manually strings to the list, and voila, it worked.

How it was before:

protected override System.Drawing.Bitmap GetThumbnailImage(uint width)
{
    try
    {
        System.Drawing.Bitmap bmp;
        var previewLines  = new List<string>();
        using (var reader = new StreamReader(SelectedItemStream))
        {
            //  Read up to ten lines of text.
            for (int i = 0; i < 10; i++)
           {
                var line = reader.ReadLine();
                if (line == null)
                    break;
                previewLines.Add(line);
            }
        }
        bmp = CreateThumbnailForText(previewLines, width);
        return bmp;
    }
    catch (Exception exception)
    {
        return null;
    }
}

How it is now:

protected override System.Drawing.Bitmap GetThumbnailImage(uint width)
{
    try
    {
        System.Drawing.Bitmap bmp;

        var previewLines  = new List<string>();
        previewLines.Add("Natan");
        previewLines.Add("atanN");
        previewLines.Add("tanNa");
        
        bmp = CreateThumbnailForText(previewLines, width);
        return bmp;
    }
    catch (Exception exception)
    {
        return null;
    }
}

natan-fernandes avatar Oct 19 '20 16:10 natan-fernandes

Still having same issue when using

using (var reader = new StreamReader(SelectedItemStream)){}

Yongjun042 avatar May 22 '21 12:05 Yongjun042

I think something went wrong when doing something with reader, it trigger catch exception, and occur this error. Don't know why but it suddenly worked by commenting out some line and testing a lot of times.

Yongjun042 avatar May 23 '21 03:05 Yongjun042