Bitmap.Save - how do I overcome an InteropServices.ExternalException e

GRoston

New member
Power User
VIP
Local time
1:31 PM
Messages
374
Following is a very simple program, extracted from my 'real' program. that demonstrates the problem. (For those interested, the jpg file is of dimension 200x200 - any jpg will work.) When I run the program within Visual Studio it works just fine. However, when I run the program stand-alone, I get an InteropServices.ExternalException error. I have searched on this and have tried a variety of things, but I keep getting the error.

Here is the 'entire' program:
Code:
Imports System.Drawing.Imaging
Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
        Dim imgLI As Image
        imgLI = New Bitmap(Image.FromFile("C:\Users\Gerry\Desktop\SmileFace.jpg"), 150, 150)
        Dim bmp As New Bitmap(250, 250)
        Using bmp
            Dim gr As Graphics = Graphics.FromImage(bmp)
            gr.Clear(Color.WhiteSmoke)
            gr.DrawImage(imgLI, 25, 25)
            bmp.Save("C:\Users\Gerry\Desktop\foobar.jpg", ImageFormat.Jpeg)
            gr.Dispose() ' this line before or after the above does not matter
        End Using
        Application.Exit()
    End Sub
End Class
Please modify this program such that it works as stand-alone. Many thanks.
 

My Computer

Computer type
PC/Desktop
OS
Windows 7 x64 Pro
CPU
Core i7 860 @ 3.8 GHz
Motherboard
MSI P55-GD80
Memory
16 GB F3-12800CL7D (DDR3 1600 7-7-7-24)
Graphics Card(s)
Sapphire Vapor-X 100283VXL Radeon HD 5770
Monitor(s) Displays
NEC LCD3090WQXi-BK
What is the full text of the error message? And on what line does it throw?
 

My Computer

Computer type
Laptop
Computer Manufacturer/Model Number
Toshiba Sattelite A665-S6092
OS
Windows 7 Ultimate x64
CPU
Intel Core i7-740QM
Memory
8 GB DDR3
Graphics Card(s)
NVIDIA GeForce 330GT
Screen Resolution
1366x768
Hard Drives
Samsung 840 SSD 500GB
1TB USB3 external HD
Cooling
Coolermaster Notepal U3 notebook cooling pad
Internet Speed
3mbps ASDL
Antivirus
ClamWin 0.98.7
Browser
Opera 12.17 x86 (main), Firefox 38 (sec), IE11 (last resort)
Good question. I left off the Loaded Assemblies stuff...
System.Runtime.InteropServices.ExternalException (0x80004005): A generic error occurred in GDI+.
at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams)
at System.Drawing.Image.Save(String filename, ImageFormat format)
at bmpSave.Form1.Form1_Load(Object sender, EventArgs e) in C:\Users\Gerry\Documents\Visual Studio 2012\Projects\Learning and Testing\bmpSave\bmpSave\Form1.vb:line 14 [[bmp.Save line]]
at System.EventHandler.Invoke(Object sender, EventArgs e)
at System.Windows.Forms.Form.OnLoad(EventArgs e)
at System.Windows.Forms.Form.OnCreateControl()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m)
at System.Windows.Forms.Form.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)

- - - Updated - - -

I further simplified the program, see below, but am still getting the same error message (when run directly, but this works fine inside Visual Studio...)!
Code:
Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
        Dim bmpOld As Bitmap = New Bitmap(Image.FromFile("C:\Users\Gerry\Desktop\SmileFace.jpg"))
        Dim bmpNew As New Bitmap(250, 250)
        bmpNew = bmpOld
        bmpNew.Save("C:\Users\Gerry\Desktop\foobar.jpg", ImageFormat.Jpeg)
        Application.Exit()
    End Sub
End Class

Weirdly, when I changed the folder to simply C:\, it worked.

- - - Updated - - -

Not sure if this counts as solved, but...

On my computer, my user files are on the D: drive. Yes, there is a symbolic link such that C:\Users\MyUser points tothe files in D:\Users\MyUser. In the code, when I changed C: to D:, everything started working. Strikes me as odd and I am guessing that you can tell me a change, probably small, to make such that to computer will treat both C: and D: equivalently. Thanks.
 

My Computer

Computer type
PC/Desktop
OS
Windows 7 x64 Pro
CPU
Core i7 860 @ 3.8 GHz
Motherboard
MSI P55-GD80
Memory
16 GB F3-12800CL7D (DDR3 1600 7-7-7-24)
Graphics Card(s)
Sapphire Vapor-X 100283VXL Radeon HD 5770
Monitor(s) Displays
NEC LCD3090WQXi-BK
Back
Top