當 m_image 是通過Gdiplus::Image::FromStream(pstm)獲取的,呼叫graphics.DrawImage( m_pImage, rcDst, l, nOffY, nPartCX, nSrcCY, UnitPixel, pIA );影像正確繪制 到界面
但是如果再次呼叫graphics.DrawImage,繪制失敗,提示例外:
0x740BB802 (KernelBase.dll)處(位于xx.exe 中)引發的例外: 0xC0000002: 未運行請求的操作。。
費解~
uj5u.com熱心網友回復:
你看看是不是使用后需要關閉流/關閉句柄/釋放資源等uj5u.com熱心網友回復:
應該不是 ,如果m_image是FromFile來的,連續呼叫多次都是正常的uj5u.com熱心網友回復:
我猜測,那個流需要重新調整一下流的posuj5u.com熱心網友回復:
流在生成m_image后就已經關閉清理了,
Image* m_image = Gdiplus::Image::FromStream(stream_);
m_image是new出來的
uj5u.com熱心網友回復:
我好像猜對了Image.FromStream 方法 (Stream) Visual Studio 2013Visual Studio 2013
本文內容已由機器翻譯。如果您連接了 Internet,請選擇“聯機查看本主題”以在可編輯模式下對照英文內容查看此頁。
向 Microsoft 發送有關本主題的反饋。 聯機在默認瀏覽器中聯機查看本主題。
從指定的資料流創建 Image。
命名空間: System.Drawing
程式集:System.Drawing(在 System.Drawing.dll 中)
語法
--------------------------------------------------------------------------------
JavaScriptC#C++F#JScriptVB以帶有顏色區分的格式查看復制到剪貼板
public static Image FromStream(
Stream stream
)
public static Image FromStream(
Stream stream
)
public:
static Image^ FromStream(
Stream^ stream
)
public:
static Image^ FromStream(
Stream^ stream
)
static member FromStream :
stream:Stream -> Image
static member FromStream :
stream:Stream -> Image
'宣告
Public Shared Function FromStream ( _
stream As Stream _
) As Image
'宣告
Public Shared Function FromStream ( _
stream As Stream _
) As Image
引數
stream
型別: System.IO.Stream
一個 Stream,包含此 Image 的資料。
回傳值
型別: System.Drawing.Image
此方法創建的 Image。
例外
--------------------------------------------------------------------------------
例外 條件
ArgumentException 該流沒有有效的影像格式
- 或 -
stream 為 null。
備注
--------------------------------------------------------------------------------
在 Image 的生存期內,必須使流保持打開。
如果用相同的流連續呼叫該方法,該流將重置為零。
說明 說明
Image 類不支持位圖中的 Alpha 透明。 若要啟用 Alpha 透明,請使用每像素 32 位的 PNG 影像。
版本資訊
--------------------------------------------------------------------------------
.NET Framework
受以下版本支持:4.5、4、3.5、3.0、2.0、1.1、1.0
.NET Framework Client Profile
受以下版本支持:4、3.5 SP1
平臺
--------------------------------------------------------------------------------
Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008(不支持服務器核心角色), Windows Server 2008 R2(支持帶 SP1 或更高版本的服務器核心角色;不支持 Itanium)
uj5u.com熱心網友回復:
跟流是否關閉確實有關,但是為啥呼叫一次是正常的呢?m_image是動態生成的,都生成了,關閉源資料流為什么會有問題呢?
uj5u.com熱心網友回復:
為什么不使用Image::Clone呢?
uj5u.com熱心網友回復:
崩潰的時候在彈出的對話框按相應按鈕進入除錯,按Alt+7鍵查看Call Stack即“呼叫堆疊”里面從上到下列出的對應從里層到外層的函式呼叫歷史。雙擊某一行可將游標定位到此次呼叫的源代碼或匯編指令處,看不懂時雙擊下一行,直到能看懂為止。uj5u.com熱心網友回復:
@hdt參考你的答案問題解決了,下面是主要代碼
Gdiplus::Image* _image = nullptr;
HGLOBAL m_hMem = GlobalAlloc(GMEM_FIXED, code.size()); //code是存放影像資料的
BYTE* pmem = (BYTE*)GlobalLock(m_hMem);
memcpy(pmem, &code[0], code.size());
GlobalUnlock(m_hMem);
IStream* pstm;
CreateStreamOnHGlobal(m_hMem, TRUE, &pstm);
_image = Gdiplus::Image::FromStream(pstm, FALSE);
。。。。
graphics.DrawImage(_image, rcDest, 6, 6, 66, 66, Unit::UnitPixel);
graphics.DrawImage(_image, rcDest, 6, 6, 66, 66, Unit::UnitPixel);
GlobalFree(m_hMem);//釋放動態申請的記憶體 (只要這行放在DrawImage之后,多次呼叫DrawImage也沒有問題)
uj5u.com熱心網友回復:
程式倒是不會崩潰,我是從輸出里面看到有例外的
你說的Image.CLone不知道怎么用
uj5u.com熱心網友回復:
Image::Clone Method--------------------------------------------------------------------------------
The Clone method creates a new Image object and initializes it with the contents of this Image object.
Syntax
Image *Clone(VOID);
Return Value
This method returns a pointer to the new Image object.
Example
The following example creates an Image object based on a JPEG file. The code creates a second Image object by cloning the first. Then the code calls the Graphics::DrawImage method twice to draw the two images.
Show Example
VOID Example_Clone(HDC hdc)
{
Graphics graphics(hdc);
// Create an Image object, and then clone it.
Image image1(L"Crayons.jpg");
Image* pImage2 = image1.Clone();
// Draw the original image and the cloned image.
graphics.DrawImage(&image1, 20, 20);
graphics.DrawImage(pImage2, 250, 20);
delete pImage2;
}
Method Information
Stock Implementation gdiplus.dll
Header Declared in Gdiplusheaders.h, include gdiplus.h
Import library gdiplus.lib
Minimum availability GDI+ 1.0
Minimum operating systems Windows 98/Me, Windows XP, Windows 2000, Windows NT 4.0 SP6
See Also
Bitmap, Image::FromFile, Image::FromStream, Image Constructors, Loading and Displaying Bitmaps, Drawing, Positioning, and Cloning Images
--------------------------------------------------------------------------------
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/92540.html
標籤:界面
上一篇:如何實作透明編輯框文字重疊問題
