全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:2378
推到 Plurk!
推到 Facebook!

有關出現Access violation at address 的問題

答題得分者是:dominicx
qqpop
一般會員


發表:2
回覆:0
積分:0
註冊:2008-12-27

發送簡訊給我
#1 引用回覆 回覆 發表時間:2009-07-01 17:17:35 IP:140.135.xxx.xxx 訂閱
為何為出現Access violation at address 這樣的問題呢?
我用的是512*512的圖片
而有問題的地方出現在 int RValue=255-ptr[x*3 2]; 這裡
有人可以幫我解答嗎? 謝謝
================引 用http://delphi.ktop.com.tw/board.php?cid=31&fid=79&tid=40159 文 章===程式的底片部份
[code cpp]
#include
#include <math.h><br />#pragma hdrstop
#include "ImageProcess.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
if (OpenPictureDialog1->Execute())
Image1->Picture->LoadFromFile(OpenPictureDialog1->FileName);
}
//-----------------------------------------------
void __fastcall TForm1::Button13Click(TObject *Sender)
{
if (Image1->Picture->Bitmap->Empty==true)
ShowMessage("請載入圖檔!!");
Graphics::TBitmap* BMP=new Graphics::TBitmap();
BMP->Assign(Image1->Picture->Bitmap );
Byte* ptr=NULL;
for (int y=0;yHeight ;y )
{
ptr=(Byte*)BMP->ScanLine[y];
for (int x=0;xWidth ;x )
{
int RValue=255-ptr[x*3 2];
int GValue=255-ptr[x*3 1];
int BValue=255-ptr[x*3];
ptr[x*3 2]=RValue;
ptr[x*3 1]=GValue;
ptr[x*3] =BValue;
}
}
Image2->Picture->Assign(BMP);
delete BMP;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
if (Image2->Picture->Bitmap->Empty==true)
ShowMessage("沒有可以存檔的畫面喔!!");
else
if (SavePictureDialog1->Execute())
Image2->Picture->SaveToFile(SavePictureDialog1->FileName);
}
//--------------------------------------------------
[/code]
編輯記錄
qqpop 重新編輯於 2009-07-01 17:18:33, 註解 無‧
taishyang
站務副站長


發表:377
回覆:5490
積分:4563
註冊:2002-10-08

發送簡訊給我
#2 引用回覆 回覆 發表時間:2009-07-01 18:57:52 IP:118.169.xxx.xxx 訂閱
x超出合理範圍導致ptr指標存取到錯誤的記憶體
dominicx
一般會員


發表:6
回覆:10
積分:13
註冊:2007-05-07

發送簡訊給我
#3 引用回覆 回覆 發表時間:2009-08-10 20:38:24 IP:222.156.xxx.xxx 訂閱

[code cpp]
for (int y=0;yHeight ;y )
{
ptr=(Byte*)BMP->ScanLine[y];
for (int x=0;xWidth ;x )
{
int RValue=255-ptr[x*3 2];
int GValue=255-ptr[x*3 1];
int BValue=255-ptr[x*3];
ptr[x*3 2]=RValue;
ptr[x*3 1]=GValue;
ptr[x*3] =BValue;
}
}
[/code]

你先前提到圖案的大小是512x512
在ptr=(Byte*)BMP->ScanLine[y]; 這行表示你的ptr指標指到了圖片中Line[y]的位置
長度為512 (因為寬是512)
你在ptr[x*3 2] 中的x最大可以跑到512
而512*3 2遠超過於你指標所指的位置的長度512
你可能要回去重新檢查一下邏輯
編輯記錄
dominicx 重新編輯於 2009-08-10 20:40:20, 註解 無‧
系統時間:2024-04-26 7:57:43
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!