Image aus Buffer zeichnen
-
Hallo,
ich möchte eine Bitmap aus einem File in einen Buffer schreiben und dann als
Bild ausgeben.Image1->Picture->LoadFromFile("Bitmap1.bmp"); //Funktioniert
Wie kann ich nun aber Image1 eine Bitmap aus dem Speicher zuweisen?
-
Ich habe nun folgendes gemacht, was ganz gut funkt.
Ich lese von Winamp die aktuelle Position des Tracks in ms und gebe diese
dann über meine selbstgezeichneten 7Segment Anzeigen aus.
Das ganze scheint mir etwas umständlich, gibs vieleicht eine einfachere
Lösung?HWND hwndWinamp; Graphics::TBitmap* Bmp0; Graphics::TBitmap* Bmp1; Graphics::TBitmap* Bmp2; Graphics::TBitmap* Bmp3; Graphics::TBitmap* Bmp4; Graphics::TBitmap* Bmp5; Graphics::TBitmap* Bmp6; Graphics::TBitmap* Bmp7; Graphics::TBitmap* Bmp8; Graphics::TBitmap* Bmp9; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { Bmp0 = new Graphics::TBitmap(); Bmp1 = new Graphics::TBitmap(); Bmp2 = new Graphics::TBitmap(); Bmp3 = new Graphics::TBitmap(); Bmp4 = new Graphics::TBitmap(); Bmp5 = new Graphics::TBitmap(); Bmp6 = new Graphics::TBitmap(); Bmp7 = new Graphics::TBitmap(); Bmp8 = new Graphics::TBitmap(); Bmp9 = new Graphics::TBitmap(); Bmp0->LoadFromFile("Bitmap0.bmp"); Bmp1->LoadFromFile("Bitmap1.bmp"); Bmp2->LoadFromFile("Bitmap2.bmp"); Bmp3->LoadFromFile("Bitmap3.bmp"); Bmp4->LoadFromFile("Bitmap4.bmp"); Bmp5->LoadFromFile("Bitmap5.bmp"); Bmp6->LoadFromFile("Bitmap6.bmp"); Bmp7->LoadFromFile("Bitmap7.bmp"); Bmp8->LoadFromFile("Bitmap8.bmp"); Bmp9->LoadFromFile("Bitmap9.bmp"); hwndWinamp = FindWindow("Winamp v1.x",NULL); TrackBar1Change(NULL); } //--------------------------------------------------------------------------- void __fastcall TForm1::TrackBar1Change(TObject *Sender) { SendMessage(hwndWinamp,WM_USER,((TrackBar1->Position-255)*-1),122); //Winamp Lautstärke } //--------------------------------------------------------------------------- void __fastcall TForm1::Timer1Timer(TObject *Sender) { unsigned int time; time =(SendMessage(hwndWinamp,WM_USER,0,105)/100);//Aktuelle Trackzeit in ms Edit1->Text = time; time = hex_to_dec(time); draw_num(Image1,(time&0x0f)); draw_num(Image2,(time>>4)&0x0f); draw_num(Image3,(time>>8)&0x0f); draw_num(Image4,(time>>12)&0x0f); draw_num(Image5,(time>>16)&0x000f); } //--------------------------------------------------------------------------- int __fastcall TForm1::hex_to_dec(int int_num) { int result; asm { mov ecx,0 mov ebx,10 mov edi,0 mov eax,int_num next: xor edx,edx div ebx and edx,0x0f shl edx,cl or edi,edx add ecx,4 cmp eax,0 jne next mov result,edi } return result; } //--------------------------------------------------------------------------- void __fastcall TForm1::draw_num(TImage* image,char number) { if(number == 0) { image->Picture->Bitmap=Bmp0; } else if(number == 1) { image->Picture->Bitmap=Bmp1; } else if(number == 2) { image->Picture->Bitmap=Bmp2; } else if(number == 3) { image->Picture->Bitmap=Bmp3; } else if(number == 4) { image->Picture->Bitmap=Bmp4; } else if(number == 5) { image->Picture->Bitmap=Bmp5; } else if(number == 6) { image->Picture->Bitmap=Bmp6; } else if(number == 7) { image->Picture->Bitmap=Bmp7; } else if(number == 8) { image->Picture->Bitmap=Bmp8; } else if(number == 9) { image->Picture->Bitmap=Bmp9; } } //--------------------------------------------------------------------------- void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action) { delete Bmp0; delete Bmp1; delete Bmp2; delete Bmp3; delete Bmp4; delete Bmp5; delete Bmp6; delete Bmp7; delete Bmp8; delete Bmp9; }
-
Hallo
Wie kann ich nun aber Image1 eine Bitmap aus dem Speicher zuweisen?
...->Canvas->Draw(x,y,TBitmap);
zu deinem Programm :
Mach ein Array aus deinen Bitmaps :Graphics::TBitmap* Bmps[10]; ... for (int Lv = 0; Lv < 10; Lv++) { Bmps[Lv] = new Graphics::TBitmap(); Bmps[Lv]->LoadFromFile("Bitmap" + IntToStr(Lv) + ".bmp"); } ... image->Picture->Bitmap=Bmps[number]; ... for (int Lv = 0; Lv < 10; Lv++) { delete Bmps[Lv]; }außerdem solltest du dir mal switch anschauen.
bis bald
akari
-
hermes schrieb:
gibs vieleicht eine einfachere Lösung?
Benutze eine ImageList (Reiter 'Win32').
-
Danke für den Hinweis.
Gibt es eigentlich auch eine andere Lösung die Bitmaps zu schreiben, bei der
o.a. Methode flackert die Anzeige ständig.
-
Hallo
schau dir diesen Thread an :
http://www.c-plusplus.net/forum/viewtopic.php?t=93888bis bald
akari
-
Das ganze ohne flackern und wesentlich kürzer.
WND hwndWinamp; Graphics::TBitmap* Bmps[10]; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { hwndWinamp = FindWindow("Winamp v1.x",NULL); TrackBar1Change(NULL); for (int Lv = 0; Lv < 10; Lv++) { Bmps[Lv] = new Graphics::TBitmap(); Bmps[Lv]->LoadFromFile("Bitmap" + IntToStr(Lv) + ".bmp"); } DoubleBuffered = true; } //--------------------------------------------------------------------------- void __fastcall TForm1::draw_num(TImage* image,char number) { image->Picture->Bitmap =Bmps[number]; } //--------------------------------------------------------------------------- void __fastcall TForm1::TrackBar1Change(TObject *Sender) { SendMessage(hwndWinamp,WM_USER,((TrackBar1->Position-255)*-1),122); } //--------------------------------------------------------------------------- void __fastcall TForm1::Timer1Timer(TObject *Sender) { unsigned int time; time =(SendMessage(hwndWinamp,WM_USER,0,105)/100); time = hex_to_dec(time); draw_num(Image1,(time&0x0f)); draw_num(Image2,(time>>4)&0x0f); draw_num(Image3,(time>>8)&0x0f); draw_num(Image4,(time>>12)&0x0f); draw_num(Image5,(time>>16)&0x000f); } //--------------------------------------------------------------------------- int __fastcall TForm1::hex_to_dec(int int_num) { int result; asm { mov ecx,0 mov ebx,10 mov edi,0 mov eax,int_num next: xor edx,edx div ebx and edx,0x0f shl edx,cl or edi,edx add ecx,4 cmp eax,0 jne next mov result,edi } return result; } //--------------------------------------------------------------------------- void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action) { for (int Lv = 0; Lv < 10; Lv++) { delete Bmps[Lv]; } }Danke für die Hilfe.
-
DoubleBuffered muss nur einmal gesetzt werden.
Und TImageList solltest du dir trotzdem nochmal ansehen.
-
Wie würde ich nun eine Komponente "7Segment" erstellen.
Was müsste ich in der überschriebenen Methode Paint tun, wenn anhand
der Eigenschaft Number die entsprechende Bitmap dargestellt werden soll?
-
Hallo
Wie würde ich nun eine Komponente "7Segment" erstellen.
Was müsste ich in der überschriebenen Methode Paint tun, wenn anhand
der Eigenschaft Number die entsprechende Bitmap dargestellt werden soll?Ich würde von TImage ableiten. Als __property
- eine Referenz auf eine externe TImageList, die alle Bitmaps enthält. Ohne zugewiesene ImageListe entweder leer oder Exception
- unsingned int als Wert, der gerade dargestellt wird. Im Setter dieser Property kannst du (wenn TImageList vorhanden ist) das entsprechende Bitmap in das eigentliche Bitmap des TImages schreiben (TImage->Canvas->Draw(0,0,<Bitmap für neuen Wert>).bis bald
akari
-
Habe mit ImageList rumprobiert und kein vernüftiges Ergebniss bekommen.
Mit doppelklick auf die ImageList Komponente eine Bitmap hinzugefügt und
dann mitImageList1->GetBitmap(0,Image1->Picture->Bitmap);
ausgegeben.
Das Ergebniss sieht nicht so wie erwartet aus (zu klein und verzerrt).
-
Hi,
hasst du auch Width und Height der ImageList angepasst?MfG
Alexander Sulfrian