Reputation:
In WPF I have a Bitmap coming from a library which deals with my webcam
Bitmap bmp = webCameraControl.GetCurrentImage();
now I have to save it but bmp.Save("C:/img.jpg"); fails So I tried
bmp.Save("C:/img.jpg", ImageFormat.Jpeg);
but it didn't work and got
"Generic GDI+ error".
Thanks
---ADD---
So if I put
bmp.Save("C:\\img.jpg", ImageFormat.Jpeg);
bmp.Save(@"C:\img.jpg", ImageFormat.Jpeg);
I get error. But if I put
bmp.Save("img.jpg", ImageFormat.Jpeg);
That works!! I can't understand!=?!?
Upvotes: 0
Views: 20927
Reputation: 113
You can save the bitmap into a stream and convert it into base64String. Once you have this its up to you what you want to do with it.
bitmap.Save(stream, ImageFormat.png);
return Convert.ToBase64String(stream.ToArray());
Upvotes: 0
Reputation: 1
You can also do like below:
private void RotateAndSaveImage(int pageNumber, double angleOfRotation)
{
EnsureNotDisposed();
// create the encoder
BitmapEncoder encoder = BitmapEncoder.Create(Decoder.CodecInfo.ContainerFormat);
// copy the destination frames
foreach (BitmapFrame frame in Decoder.Frames)
encoder.Frames.Add(frame);
BitmapFrame oldFrame = encoder.Frames[pageNumber - 1];
// Create the TransformedBitmap to use as the Image source.
TransformedBitmap tb = new TransformedBitmap();
// Properties must be set between BeginInit and EndInit calls.
tb.BeginInit();
tb.Source = new CachedBitmap(oldFrame, BitmapCreateOptions.None, BitmapCacheOption.None);
RotateTransform transform = new RotateTransform(angleOfRotation);
tb.Transform = transform;
tb.EndInit();
encoder.Frames.RemoveAt(pageNumber - 1);
encoder.Frames.Insert(pageNumber - 1, BitmapFrame.Create(tb));
// save encoder
Save(encoder)
}
/// <summary>
/// Saves the contents of a bitmap encoder to <see cref="Stream"/>.
/// </summary>
/// <param name="encoder">
/// The encoder from which to obtain the data to save.
/// </param>
private void Save(BitmapEncoder encoder)
{
// save to a temporary stream
string tempFileName = Path.GetTempFileName();
try
{
using (FileStream temporaryStream = new FileStream(tempFileName, FileMode.OpenOrCreate))
{
if (encoder.Frames.Count > 0)
encoder.Save(temporaryStream);
// write back out to permanent stream
if (Stream.CanWrite && Stream.CanSeek)
CopyStream(temporaryStream, Stream);
else
throw new UnauthorizedAccessException();
}
}
finally
{
// Delete the temporary file
File.Delete(tempFileName);
}
}
Upvotes: 0
Reputation: 503
Have you tried another location to save the image to? I guess that this directory is protected by windows itself.
Solution:
Right click your project file on the Solution Explorer, select Add, then New item. There you can find Application Manifest File.
It creates file called app.manifest. Open it and find the tag requestedPrivileges. (like shown below)
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
<!-- ##GERMAN TEXT##
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
<requestedExecutionLevel level="highestAvailable" uiAccess="false" /> ##GERMAN TEXT##
-->
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
</requestedPrivileges>
The line you are interested in is this one:
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
It sais you execute it as the invoker ("asInvoker"). Change it to "requireAdministrator" and restart VisualStudio as administrator.
This should make it :)
I forgot to mention that I tried it with these 2 lines of code. It works for me, when I run it as administrator.
Bitmap bmp = new Bitmap(12,12);
bmp.Save("C:/img.jpg", ImageFormat.Jpeg);
Upvotes: 3
Reputation: 9397
Here is how I do it:
public static void RotateAndSaveImage(string path, WriteableBitmap bitmap, double angle)
{
try
{
if (bitmap != null)
{
var tb = new TransformedBitmap((bitmap), new RotateTransform(angle));
var src = tb as BitmapSource;
using (FileStream stream = new FileStream(path, FileMode.Create))
{
PngBitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(src));
encoder.Save(stream);
}
}
} catch {}
}
Upvotes: 0
Reputation: 2853
I'm guessing the issue you have is with the "/" in your save path. You can write it one of two ways:
bmp.Save("C:\\img.jpg", ImageFormat.Jpeg); //two backslashes escapes to a single backslash
bmp.Save(@"C:\img.jpg", ImageFormat.Jpeg); //adding @ escapes the backslash automatically
Edit I found this over at Super User, you might be able to get around using C:\
using this. Use the path "%HomeDrive%\\img.jpg"
, which is a built in path shortcut to C
drive. I don't know how reliably this works from C#, but it worked last time I tried it and it works when I tested in the file explorer right now.
Write permissions to that directory seem the most likely problem for you though. If that is the case, I don't know what can be done.
Upvotes: 1
Reputation: 2879
"Generic GDI+ error". mostly will occur if the path does not exist to the directory where you want to store it.
bmp.Save("C:\\Test\\img.jpg", ImageFormat.Jpeg);
make sure you use \\
or @"C:\Test\img.jpg"
If this all is not working you maybe have no rights to save to C:\\
Upvotes: 4