gpt4 book ai didi

C++ GDI+ 从文件中加载图像,然后在*卸载图像之前删除文件

转载 作者:搜寻专家 更新时间:2023-10-30 23:49:09 24 4
gpt4 key购买 nike

简单地说就是 jar 头上写的我正在使用 Bitmap::FromFile 从文件加载位图,但之后我想将其从磁盘中删除。
问题是,Bitmap::FromFile 绝对锁定文件以防止任何更改/删除,直到加载的图像被卸载

这是因为我将位图存储在二进制文件中,所以我想按以下顺序进行:
1. 从二进制文件中提取图像
2.加载图片
3.删除#1中提取的文件
(只是对我的图像资源的一些基本保护,我只是不想让它们坐在我的程序目录中)

Bitmap::FromFile 仍然锁定文件以防止删除,即使像我尝试的那样从文件中克隆加载的图像也是如此:

    Bitmap* tempbmp = Bitmap::FromFile(fileanddir.c_str(),false);
Rect temprect( 0, 0, tempbmp->GetWidth(), tempbmp->GetHeight() );

// make the image to be used as a clone to the temporary
// bitmap to avoid file locking
image_to_be_used = tempbmp->Clone(temprect, PixelFormatDontCare);

// delete temporary loaded bitmap since it shouldn't be needed
delete tempbmp;

// delete the file itself, too bad the file is locked
int theresult = remove(tocharptr(fileanddir));
// returns -1, also: manually deleting at this point gives the error
// that the file is being used by another person/program

知道如何加载位图或以某种方式将其复制到内存中以便文件本身不会被锁定吗?
(所以我可以在加载后删除它)

最佳答案

你可以这样做

Gdiplus::Bitmap* LoadImageFromFileWithoutLocking(const WCHAR* fileName) {
using namespace Gdiplus;
Bitmap src( fileName );
if ( src.GetLastStatus() != Ok ) {
return 0;
}
Bitmap *dst = new Bitmap(src.GetWidth(), src.GetHeight(), PixelFormat32bppARGB);

BitmapData srcData;
BitmapData dstData;
Rect rc(0, 0, src.GetWidth(), src.GetHeight());

if (src.LockBits(& rc, ImageLockModeRead, PixelFormat32bppARGB, & srcData) == Ok)
{
if ( dst->LockBits(& rc, ImageLockModeWrite, PixelFormat32bppARGB, & dstData) == Ok ) {
uint8_t * srcBits = (uint8_t *) srcData.Scan0;
uint8_t * dstBits = (uint8_t *) dstData.Scan0;
unsigned int stride;
if (srcData.Stride > 0) {
stride = srcData.Stride;
} else {
stride = - srcData.Stride;
}
memcpy(dstBits, srcBits, src.GetHeight() * stride);

dst->UnlockBits(&dstData);
}
src.UnlockBits(&srcData);
}
return dst;
}

关于C++ GDI+ 从文件中加载图像,然后在*卸载图像之前删除文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4978419/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com