Image不能动态改变width,heigth的情况??改了也无法在增大后的区域绘图!!(100分)

  • 主题发起人 主题发起人 philips
  • 开始时间 开始时间
我的程序 是先在image1上画线, 当鼠标在Image1上拖动时,动态绘制巨型橡

皮线,松开鼠标后,确定了Image2的位置和大小,我将 Image2 所覆盖的

Image1区域的曲线复制到 Image2上, 然而,不能按照动态变化的 Image2 的大小

来复制 Image1 对应区域的曲线。

问题就是这样, 请大侠关注以下。拜托!!!
 
试一试这个:
Image2.picture.Height := aHeight;
Image2.picture.Width := aWidth;
 

这个恐怕不行,因为编译时显示:

E2247'Tpicture::Height' is not accessible.
 
我的意思就是直接改 image 的picture 的 Height 和 Width 属性,
应该可以的,但忘了怎么改了,我用过的。
 
调整Image1大小后,要相应调整Image1->Picture->Bitmap的大小

Image1->Width = Image1->Width * 2;
Image1->Height = Image1->Height * 2;
Image1->Picture->Bitmap->Width = Image1->Width;
Image1->Picture->Bitmap->Height = Image1->Height;
 
我的程序加上这两句后,运行光带停在第一句上,时显示:

" Read of address 00000000"
 
我实验过的,绝对没错,呵呵。不过,我的image1不是动态创建的。
你把源代码给我看看:sqh@dna.sibc.ac.cn,记得带上.h文件。
前两次都没有带.h文件,没法查错。
 
faint!
你的源码中x,y弄错了:

//先加上这两句
Image2->Picture->Bitmap->Height = abs(EndPt.y - StartPt.y);
Image2->Picture->Bitmap->Width = abs(EndPt.x - StartPt.x);

Image2->Left = (StartPt.x>EndPt.x ? EndPt.x :StartPt.x) +ileft;
Image2->Top = (StartPt.y>EndPt.y ? EndPt.y :StartPt.y) +itop;

//这里应该是i对应Width,j对应height。
for(i=0;i<Image2->Width;i++)
for(j=0;j<Image2->Height;j++)

还有,在Child0frm.cpp应该有:
TfrmChild0 *frmChild0;
你的源码中怎么没有?

 
首先 ,非常感谢 DreamTiger

我按您说的加上那两行之后,没发现效果有改善,

另外,我还是以为 这里应该是i对应Height,j对应Width。

我把代码发给你,帮我调试以下。

先行谢过大侠!
 
我就是用你上次发给我的代码试验成功了再写的:
下面是我改过后的文件:

Child0form.h
//---------------------------------------------------------------------------
#ifndef Child0formH
#define Child0formH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
//---------------------------------------------------------------------------
class TfrmChild0 : public TForm
{
__published: // IDE-managed Components
TImage *Image1;
TImage *Image2;
void __fastcall FormCreate(TObject *Sender);
void __fastcall Image1MouseUp(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y);
void __fastcall Image1MouseDown(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y);
void __fastcall Image1MouseMove(TObject *Sender, TShiftState Shift,
int X, int Y);
void __fastcall Image2MouseMove(TObject *Sender, TShiftState Shift,
int X, int Y);
void __fastcall Image2MouseDown(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y);
void __fastcall Image2MouseUp(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y);
private: // User declarations
public: // User declarations
__fastcall TfrmChild0(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TfrmChild0 *frmChild0;
//---------------------------------------------------------------------------
#endif


Child0form.cpp
//---------------------------------------------------------------------------
#include <vcl.h>
#include <math.h>
#pragma hdrstop
#include "Child0form.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TfrmChild0 *frmChild0;

TPoint StartPt,EndPt;
bool bDrawing = False, bDraging = False;

//---------------------------------------------------------------------
__fastcall TfrmChild0::TfrmChild0(TComponent *Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------

void __fastcall TfrmChild0::Image1MouseUp(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
int i,j;

if( bDrawing )
{
bDrawing = False;
Image1->Canvas->Rectangle(StartPt.x,StartPt.y, EndPt.x,EndPt.y);
Image1->Canvas->Pen->Mode = pmCopy;
Image1->Canvas->Pen->Color = clBlack;
Image1->Canvas->Brush->Style = bsClear;

int itop = Image1->Top;
int ileft = Image1->Left;

Image2->Height = abs(EndPt.y - StartPt.y);
Image2->Width = abs(EndPt.x - StartPt.x);
Image2->Picture->Bitmap->Height = abs(EndPt.y - StartPt.y);
Image2->Picture->Bitmap->Width = abs(EndPt.x - StartPt.x);
Image2->Left = (StartPt.x>EndPt.x ? EndPt.x :StartPt.x) +ileft;
Image2->Top = (StartPt.y>EndPt.y ? EndPt.y :StartPt.y) +itop;

for(i=0;i<Image2->Width;i++)
for(j=0;j<Image2->Height;j++)
{
if( Image1->Canvas->Pixels[i+ileft][j+itop]==clRed )
Image2->Canvas->Pixels[j]=clGreen;
else
Image2->Canvas->Pixels[j]=clRed;
}

Image2->Visible = True;
}
}
//---------------------------------------------------------------------------

void __fastcall TfrmChild0::Image1MouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
Image2->Visible = False;
Image1->Canvas->Pen->Color = clWhite;
Image1->Canvas->Rectangle(StartPt.x,StartPt.y, EndPt.x,EndPt.y);

bDrawing = True;
StartPt = TPoint(X,Y);
EndPt = StartPt;

Image1->Canvas->Pen->Mode = pmNot;
Image1->Canvas->Pen->Color = clRed;
Image1->Canvas->Brush->Style =bsClear;
Image1->Canvas->Pen->Style = psDot;
}
//---------------------------------------------------------------------------

void __fastcall TfrmChild0::Image1MouseMove(TObject *Sender,
TShiftState Shift, int X, int Y)
{
if( bDrawing &amp;&amp; ((EndPt.x != X)||(EndPt.y != Y)) )
{
Image1->Canvas->Rectangle(StartPt.x,StartPt.y, EndPt.x,EndPt.y);
Image1->Canvas->Rectangle(StartPt.x,StartPt.y, X,Y);
EndPt.x = X;
EndPt.y = Y;
}
}
//---------------------------------------------------------------------------

void __fastcall TfrmChild0::Image2MouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
StartPt.x = X;
StartPt.y = Y;
bDraging = True;
}
//---------------------------------------------------------------------------

void __fastcall TfrmChild0::Image2MouseMove(TObject *Sender,
TShiftState Shift, int X, int Y)
{ if( bDraging )
{
Image2->Left = Image2->Left +X - StartPt.x;
Image2->Top = Image2->Top + Y - StartPt.y;
Image2->Hint = "X:"+IntToStr(Image2->Left)+" Y:"+IntToStr(Image2->Top);
}

}
//---------------------------------------------------------------------------

void __fastcall TfrmChild0::Image2MouseUp(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
bDraging = False;
}
//---------------------------------------------------------------------------

void __fastcall TfrmChild0::FormCreate(TObject *Sender)
{
int i,y;

for (i=1;i<Image1->Height; i++)
{
y=100*sin(i/180.0*3.1415);
Image1->Canvas->Pixels[y+200] = clRed;
}

}
//---------------------------------------------------------------------------
 
重设bitmap大小
image1.width:=newvalue;
image1.height:=newvalue;
image1.picture.bitmap.width:=image1.width;
image1.picture.bitmap.height:=image1.width;
by the way:这个问题是我登录本论坛查询找到的第一个问题答案.
 
DreamTiger 大侠:

谢谢您!!


我发现,按您的方法,在单独的测试窗体中,方框的大小问题解决了,

不知您是否注意到 Image1 上的曲线没能正确的画在 Image2上,这是否为

Image 构件的 BUG? ,另外,我在另外一个 MDI窗体,上做同样的事,方

框的大小、及曲线复制都有问题, 时好时坏,是否 Inprise(Borland)

不提倡这种方法, Image2 能否透明?
 
上面的正文部分不对,作废。

再次谢过 Dreamtiger大侠,按您的方法,问题决了。我发现您是本论坛的一流

高手,以后有问题还请您热心帮助。

Philips
 
对其他大侠的 光临表示感谢!!
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
1K
DelphiTeacher的专栏
D
后退
顶部