C
C#9mo ago
Frostyio

✅ 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
leowest
leowest9mo ago
not sure if that is a typo but u have a space before Base that is not a valid folder name AFAIK
Frostyio
FrostyioOP9mo ago
yes its a typo i fixed it
leowest
leowest9mo ago
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
Frostyio
FrostyioOP9mo ago
i read online i shouldnt in this case but may have been false
leowest
leowest9mo ago
online where? u get the file name u no longer need the file dialog, it could be one of the reason its its throwing
Frostyio
FrostyioOP9mo ago
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 try
leowest
leowest9mo ago
well 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
Frostyio
FrostyioOP9mo ago
oh i see do i need to copy it somehow?
leowest
leowest9mo ago
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
Frostyio
FrostyioOP9mo ago
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
leowest
leowest9mo ago
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?
Frostyio
FrostyioOP9mo ago
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
leowest
leowest9mo ago
then I dont think so
Frostyio
FrostyioOP9mo ago
yeah i dont think i could thanks
leowest
leowest9mo ago
u should be able to just call
pboxGEN.Image.Save(filePath,ImageFormat.Jpeg);
pboxGEN.Image.Save(filePath,ImageFormat.Jpeg);
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
Frostyio
FrostyioOP9mo ago
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);
leowest
leowest9mo ago
ah true nicely spotted make sure u dispose of those 😉
Frostyio
FrostyioOP9mo ago
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?
leowest
leowest9mo ago
maybe if u change the background color of the picturebox it self?
Frostyio
FrostyioOP9mo ago
yeah i was thinking maybe its an option
leowest
leowest9mo ago
pbox.BackColor = Color.Transparent or something along those lines
Frostyio
FrostyioOP9mo ago
ill check the form pbox properties on visual studio as well
leowest
leowest9mo ago
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?
Frostyio
FrostyioOP9mo ago
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
leowest
leowest9mo ago
cards for what?
Frostyio
FrostyioOP9mo ago
and yes on the program i can see a preview of the card before saving card farmers, game im developing
leowest
leowest9mo ago
ah ok nice gl
Frostyio
FrostyioOP9mo ago
very simplistic designs also i set it to transparent but i still get the black back
leowest
leowest9mo ago
I imagined it was for a game but what code are u using to save things now
Frostyio
FrostyioOP9mo ago
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
leowest
leowest9mo ago
pboxGEN.Image.Save(filePath,ImageFormat.Jpeg); you're using just this to save?
Frostyio
FrostyioOP9mo ago
no
leowest
leowest9mo ago
does your preview have black anywhere?
Frostyio
FrostyioOP9mo ago
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 graphic
leowest
leowest9mo ago
could be
Frostyio
FrostyioOP9mo ago
oh well ill mess around with it i got my original problem solved so thank you
leowest
leowest9mo ago
try loading a transparent image directly to the pbox and then save it
Frostyio
FrostyioOP9mo ago
oh smart
leowest
leowest9mo ago
if it does not get any black background u know where its coming from
Frostyio
FrostyioOP9mo ago
alright so
Frostyio
FrostyioOP9mo ago
No description
Frostyio
FrostyioOP9mo ago
thats the image i used
Frostyio
FrostyioOP9mo ago
No description
Frostyio
FrostyioOP9mo ago
thats what was produced
leowest
leowest9mo ago
let me give it a try moment
Frostyio
FrostyioOP9mo ago
lol i think it is the graphic fs
leowest
leowest9mo ago
I mean I did say load it directly to the pbox
Frostyio
FrostyioOP9mo ago
yeah can i draw image onto a pbox direclty with more than 1 image
leowest
leowest9mo ago
well the pbox retains the transparency so I assume the graphics adds it
Frostyio
FrostyioOP9mo ago
i should be able to remove it
leowest
leowest9mo ago
ok so yeah u need to set the transparency on the graphics and also use Png instead
private Bitmap _bmp;
private void button4_Click(object sender, EventArgs e)
{
if (icon is null || background is null) return;

var src1 = background.Image;
var src2 = icon.Image;

if (_bmp is not null) _bmp.Dispose();
_bmp = new Bitmap(src1.Width + src2.Width, src1.Height, PixelFormat.Format32bppPArgb);
using var g = Graphics.FromImage(_bmp);
{
g.Clear(Color.Transparent);
g.DrawImage(src1, new Rectangle(0, 0, src1.Width, src1.Height));
g.DrawImage(src2, new Rectangle(0, 0, src2.Width, src2.Height));
combination.Image = _bmp;
combination.Invalidate();
}
}
private Bitmap _bmp;
private void button4_Click(object sender, EventArgs e)
{
if (icon is null || background is null) return;

var src1 = background.Image;
var src2 = icon.Image;

if (_bmp is not null) _bmp.Dispose();
_bmp = new Bitmap(src1.Width + src2.Width, src1.Height, PixelFormat.Format32bppPArgb);
using var g = Graphics.FromImage(_bmp);
{
g.Clear(Color.Transparent);
g.DrawImage(src1, new Rectangle(0, 0, src1.Width, src1.Height));
g.DrawImage(src2, new Rectangle(0, 0, src2.Width, src2.Height));
combination.Image = _bmp;
combination.Invalidate();
}
}
Frostyio
FrostyioOP8mo ago
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
leowest
leowest8mo ago
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
MODiX
MODiX8mo ago
Use the /close command to mark a forum thread as answered
Want results from more Discord servers?
Add your server