ColorMatrix erzeugen in CLI?
-
Hallo Forum,
ich möchte gern ein PNG in meinem Programm langsam ausblenden, dafür habe ich nachgelesen das man den Alphawert erhöhen kann. Zu dem Thema habe ich diesem Artikel und diesen, gefunden leider sind die Beispiele (auch beim zweiten) nicht so einfach in CLI umzubauen. Ich scheitere an folgendem:
ColorMatrix cm = { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f };
Fehler: error C2552: 'cm' : non-aggregates cannot be initialized with initializer list
Ich habe es auch schon per mehrdimensionalem float-Array versucht das hat leider auch nicht geklappt.
Weiß jemand Rat oder kann das Beispiel in CLI übersetzen?
Gdiplus::Graphics graphics(hdc); //Handle to the device context //Load the image from a file Gdiplus::Image image(L"YourPicture.gif"); // Create an ImageAttributes object and set its color matrix. Gdiplus::ImageAttributes imageAtt; Gdiplus::ColorMatrix cm = {1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f}; cm.m[3][3]+=0.01f; //transparency imageAtt.SetColorMatrix(&cm, Gdiplus::ColorMatrixFlagsDefault, Gdiplus::ColorAdjustTypeBitmap); graphics.DrawImage(&image, Gdiplus::Rect(0, 0, image.GetWidth(), image.GetHeight()), 0, 0, image.GetWidth(), image.GetHeight(), Gdiplus::UnitPixel, &imageAtt);
Danke fürs Lesen
-
Du musst ein ColorMatrix^ erzeugen. Und am Konstruktor kannst Du ein float Array Array (array<array<float>^ >^) übergeben. Siehe hier: http://msdn.microsoft.com/en-us/library/6s4w6k67.aspx
Siehe auch hier das C# Bsp:
http://msdn.microsoft.com/de-de/library/w177ax15%28v=vs.80%29.aspx#BTW: Warum nicht einfach C#?
-
Vielen Dank für deine Antwort, genau das versuche ich ja.
array<float, 2>^ farrMatrixItems = gcnew array<float, 2>(5, 5); ColorMatrix^ coMatrix = gcnew ColorMatrix( farrMatrixItems );
Ich hab zum Programmieren leider keine Wahl ich muss CLI nehmen.
Fehler ist:
: error C2664: 'System::Drawing::Imaging::ColorMatrix::ColorMatrix(cli::array<Type,dimension> ^)' : cannot convert parameter 1 from 'cli::array<Type,dimension> ^' to 'cli::array<Type,dimension> ^'
1> with
1> [
1> Type=cli::array<float,1> ^,
1> dimension=1
1> ]
1> and
1> [
1> Type=float,
1> dimension=2
1> ]
1> and
1> [
1> Type=cli::array<float,1> ^,
1> dimension=1
1> ]
1> Cannot convert between managed arrays of different ranksIn den Beispielen ist es ein Mehrdimensionales Array 5x5, aber die Autovervollständigung sagt es soll ein Eindimensionales sein.
-
array<array<float>> so vielliecht?
-
Geht leider auch nicht. Ich habe es endlich lösen können mit den Matrixzugriff zu Fuß. Hier ein Ausschnitt
coColorMatrix->Matrix40 = 0.0f; coColorMatrix->Matrix41 = 0.0f; coColorMatrix->Matrix42 = 0.0f; coColorMatrix->Matrix43 = 0.0f;
Leider fliegt er mit ner NullPointerException in der Zeile wo ich meine ColorMatrix an das ImageAttribute hängen möchte.
iaImageAttributes->SetColorMatrix( coColorMatrix, ColorMatrixFlag::Default, ColorAdjustType::Bitmap );
Fehler: An unhandled exception of type 'System.NullReferenceException' occurred in System.Drawing.dll
Additional information: Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.Sieht jemand meinen Fehler? Ok hab den Fehler hatte im Konstruktor, was vergessen.
-
Geht leider auch nicht
Da zeigst Du nicht mal was Du gemacht hast? Obwohl Du es jetzt auf eine andere Art gelöst hast, hier noch ein Bsp. So sollte es gehen, die korrekten Werte musst Du selbst einfüllen:
array<array<float>^>^ matrix = gcnew array<array<float>^>(5); for (int i = 0; i < matrix->Length; ++i) { matrix[i] = gcnew array<float>(5); } { array<float>^ vector = matrix[0] vector[0] = 0; vcetor[1] = 0; vector[2] = 0; vcetor[3] = 0; vector[4] = 0; } { // same for matrix[1..4] } ColorMatrix^ colorMatrix = gcnew ColorMatrix(matrix); // use colorMatrix
-
Danke für den Vorschlag, welchen Codeausschnitt soll ich denn am besten Posten?
-
gast__1 schrieb:
Danke für den Vorschlag, welchen Codeausschnitt soll ich denn am besten Posten?
Denselben wie im ersten Post, aber abgeändert...
-
theta schrieb:
Denselben wie im ersten Post, aber abgeändert...
Ok. Dies legt die ColorMatrix an und hängt diese dann an ein ImageAttributes an.
ColorMatrix^ coColorMatrix = gcnew ColorMatrix(); coColorMatrix->Matrix00 = 1.0f; coColorMatrix->Matrix01 = 0.0f; coColorMatrix->Matrix02 = 0.0f; coColorMatrix->Matrix03 = 0.0f; coColorMatrix->Matrix04 = 0.0f; coColorMatrix->Matrix10 = 0.0f; coColorMatrix->Matrix11 = 1.0f; coColorMatrix->Matrix12 = 0.0f; coColorMatrix->Matrix13 = 0.0f; coColorMatrix->Matrix14 = 0.0f; coColorMatrix->Matrix20 = 0.0f; coColorMatrix->Matrix21 = 0.0f; coColorMatrix->Matrix22 = 1.0f; coColorMatrix->Matrix23 = 0.0f; coColorMatrix->Matrix24 = 0.0f; coColorMatrix->Matrix30 = 0.0f; coColorMatrix->Matrix31 = 0.0f; coColorMatrix->Matrix32 = 0.0f; coColorMatrix->Matrix33 = iAphaValue; // Alphawert coColorMatrix->Matrix34 = 0.0f; coColorMatrix->Matrix40 = 0.0f; coColorMatrix->Matrix41 = 0.0f; coColorMatrix->Matrix42 = 0.0f; coColorMatrix->Matrix43 = 0.0f; coColorMatrix->Matrix44 = 1.0f; iaImageAttributes->SetColorMatrix( coColorMatrix, ColorMatrixFlag::Default, ColorAdjustType::Bitmap );
In der Zeichenfunktion nutze ich dann:
e->DrawImage( imObjektRobot, Rectangle( -1, 0, 60, 60 ), // baue Rechteck 0, // linke Obere Ecke des Rechtecks (x-Koordinate) 0, // linke Obere Ecke des Rechtecks (y-Koordinate) 60, // Breite des Rechtecks 60, // Hoehe des Rechtecks GraphicsUnit::Pixel, // Masseinheit sind Pixeln iaImageAttributes );
imObjektRobot ist mein ImageObjekt.
Ich hoffe das hilft jemandem.
Ich geh dann ma weiterkämpfen mit dem Drehen des Bildes.