✅ Winform Bitmap.save error: cannot access the file
Error saving card: The process cannot access the file because it is being used by another process.Attempted saving at: C:/Users/Nated/Desktop/Card Farmer/Base Card Types/Rare Base.png
The debug states: Exception thrown: 'System.IO.IOException' in System.Private.CoreLib.dll
code being used:https://hatebin.com/bpzcrdwjin
I was originally recieving a GDI+ error but I am not getting it anymore and instead this, this is all the information for the error I'm aware of
54 Replies
not sure if that is a typo but u have a space before Base that is not a valid folder name AFAIK
yes its a typo i fixed it
well remove the try and catch so u can get the full stack
but file exception is most likely something to do with either file in use, or permission or folder path not existent
also u never dispose of the file dialogs
at least I dont see it happening in your code
i read online i shouldnt in this case
but may have been false
online where?
u get the file name u no longer need the file dialog, it could be one of the reason its its throwing
System.IO.IOException: 'The process cannot access the file 'C:\Users\Nated\Desktop\Card Farmer\Common Card\Common Base.png' because it is being used by another process.'
after removing try and catch
some random place like stack overflow
ill trywell file dialog has nothing to do with the file issues u should dispose
what could be a problem is
u load the file to a picturebox
now the file is locked
u try to save to that same file
u get an error
oh i see
do i need to copy it somehow?
the bitmap can lock the file
u could read the file to a memorystream and load the memorystream if you're trying to reuse the same filename
i dont think i know how to do that
cardMap = (Bitmap)pboxGEN.Image;
could this cause it too become locked?
on any guides or things online i see them do similar things or stuff i tried without error
https://stackoverflow.com/a/18172171
either of those answers would work
one uses memorystream the other uses a copy so neither would lock the file
it depends how u load the image to the picturebox
and from what file u load and save
are u saving to the same file u load?
no i dont believe so
i use a graphics
and it takes 2 images to build the card
background + icon
im attempting to save that as 1 image for use
then I dont think so
yeah i dont think i could
thanks
u should be able to just call
just make sure u check if directory exist and file does not exist before u save
are the crashes random or are they easy to get?
as in happens very frequent
I FOUND IT
https://hatebin.com/jiwbkyianl
latest code
Lines 79-86
i use sfd1 as the save dialog
when then i use my open dialog on accident when using it
if (sfd1.ShowDialog() == DialogResult.OK)
{
//try
//{
string filePath = ofd1.FileName;
SaveFile(pboxGEN.Image, ofd1.FileName);
ah true
nicely spotted
make sure u dispose of those 😉
i will heh
thanks for the help it got me lookin around anyway
oh btw this is a smaller issue something i can prob find online quick
my picture box when saved has a black background
any way to make that transparent?
maybe if u change the background color of the picturebox it self?
yeah i was thinking maybe its an option
pbox.BackColor = Color.Transparent or something along those lines
ill check the form pbox properties on visual studio as well
it exists in there as well
do u have a preview of what ur doing? are u making like a card game with random background and icons?
this program is meant to help me create the cards quickly and easily
my friends want to help but theyre idiots so im making this
cards for what?
and yes on the program i can see a preview of the card before saving
card farmers, game im developing
ah ok nice gl
very simplistic designs
also
i set it to transparent but i still get the black back
I imagined it was for a game
but what code are u using to save things now
i notice the picture box has a lot of extra space around it
same as the code i sent
same saving
i directly use the pbox.Image instead of copying it
pboxGEN.Image.Save(filePath,ImageFormat.Jpeg);
you're using just this to save?
no
does your preview have black anywhere?
private void btnSave_Click(object sender, EventArgs e)
{
if (pboxGEN.Image != null)
{
if (sfd1.ShowDialog() == DialogResult.OK)
{
//try
//{
string filePath = sfd1.FileName;
SaveFile(pboxGEN.Image, sfd1.FileName);
}
}
}
private void SaveFile(Image fileToSave, string saveLoc)
{
string outputFileName = saveLoc;
using (MemoryStream memory = new MemoryStream())
{
using (FileStream fs = new FileStream(outputFileName, FileMode.Create, FileAccess.ReadWrite))
{
fileToSave.Save(memory, ImageFormat.Jpeg);
byte[] bytes = memory.ToArray();
fs.Write(bytes, 0, bytes.Length);
}
}
}
}
no the preview does not
its filling in the extra space on the pbox
is it cause of the graphic?
possibly can make the rest of the pbox transparent within the graphiccould be
oh well ill mess around with it
i got my original problem solved so thank you
try loading a transparent image directly to the pbox
and then save it
oh smart
if it does not get any black background u know where its coming from
alright so
thats the image i used
thats what was produced
let me give it a try moment
lol
i think it is the graphic fs
I mean I did say load it directly
to the pbox
yeah
can i draw image onto a pbox direclty with more than 1 image
well the pbox retains the transparency
so I assume the graphics adds it
i should be able to remove it
ok so yeah u need to set the transparency on the graphics
and also use Png instead
sorry for not responding but i figured it out
i was saving it as JPEG which apparently can't be transparent
i made it a png and it worked
so know you know another little fact too
yeah that is what I said above on that code 🙂 adding the transparency and to save it as png, glad u sorted it out
thanks for getting back
Use the /close command to mark a forum thread as answered