Autor Beitrag
JonnyJohnson
Hält's aus hier
Beiträge: 3



BeitragVerfasst: Fr 07.09.12 09:47 
Morschn,

ich versuche 2 Bilder "live" zu überlagern. Man wählt 2 Bilder aus, die geladen werden, dazu kann man einen Wert zur Skalierung des 2. Bildes angeben und die gewünschte Position. Die Bilder werden dann wie angegeben übereinander gezeichnet.
Das Prinzip funktioniert soweit ohne Probleme, allerdings nur mit einem Zwischenspeicherschritt.
Erst mal der Code:

Code:

ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
    using System;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.IO;
    using System.Windows;
    using System.Windows.Media;
    using Image = System.Drawing.Image;
    using MessageBox = System.Windows.MessageBox;
    using Rectangle = System.Drawing.Rectangle;
     
    namespace Convertilon
    {
    public partial class MainWindow
    {
    public MainWindow()
    {
    InitializeComponent();
    }
     
    private void Changilon(object sender, RoutedEventArgs e)
    {
    var b = new ImageSourceConverter();
    var bild1 = Image.FromFile(file1.Text);
    var bild2 = SetImgOpacity(new Bitmap(file2.Text), (float)slider1.Value);
    var scaling = Math.Round(slider1.Value);
    var pos = 0;
    int xpos;
    int ypos;
     
    if (rbLinks.IsChecked == true)
    pos = 1;
    if (rbObenLinks.IsChecked == true)
    pos = 2;
    if (rbOben.IsChecked == true)
    pos = 3;
    if (rbObenRechts.IsChecked == true)
    pos = 4;
    if (rbRechts.IsChecked == true)
    pos = 5;
    if (rbUntenRechts.IsChecked == true)
    pos = 6;
    if (rbUnten.IsChecked == true)
    pos = 7;
    if (rbUntenLinks.IsChecked == true)
    pos = 8;
     
    switch (pos)
    {
    case 1:
    xpos = 0;
    ypos = (int)Math.Round(bild1.Height/2d) - (int)Math.Round(bild2.Height/(2*scaling));
    break;
    case 2:
    xpos = 0;
    ypos = 0;
    break;
    case 3:
    xpos = (int) Math.Round(bild1.Width/2d) - (int) Math.Round(bild2.Width/(2*scaling));
    ypos = 0;
    break;
    case 4:
    xpos = bild1.Width - (int) Math.Round(bild2.Width/scaling);
    ypos = 0;
    break;
    case 5:
    xpos = bild1.Width - (int) Math.Round(bild2.Width/scaling);
    ypos = (int) Math.Round(bild1.Height/2d) - (int) Math.Round(bild2.Height/(2*scaling));
    break;
    case 6:
    xpos = bild1.Width - (int) Math.Round(bild2.Width/scaling);
    ypos = bild1.Height - (int) Math.Round(bild2.Height/scaling);
    break;
    case 7:
    xpos = (int) Math.Round(bild1.Width/2d) - (int) Math.Round(bild2.Width/(2*scaling));
    ypos = bild1.Height - (int) Math.Round(bild2.Height/scaling);
    break;
    case 8:
    xpos = 0;
    ypos = bild1.Height - (int) Math.Round(bild2.Height/scaling);
    break;
    default:
    xpos = 0;
    ypos = 0;
    break;
    }
    var cmxPic = new ColorMatrix { Matrix33 = 1 };
    ImageAttributes iaPic = new ImageAttributes();
    iaPic.SetColorMatrix(cmxPic, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
    var g = Graphics.FromImage(bild1); //background
    g.DrawImage(bild2, new Rectangle(xpos, ypos, (int)Math.Round(bild2.Width/scaling), (int)Math.Round(bild2.Height/scaling)), 00, bild2.Width, bild2.Height, GraphicsUnit.Pixel, iaPic);
     
    try
    {
    var name1 = genName();
    bild1.Save(name1 + ".jpg");
    var bild = b.ConvertFromString(name1 + ".jpg");
    image3.Source = (ImageSource)bild;
    bild1.Dispose();
    }
    catch
    {
    MessageBox.Show("Zoooonk");
    }
    finally
    {
    bild1.Dispose();
    }
     
    }
     
    public string genName()
    {
    var b = new Random();
    var alphabet = new string[26]
    {
    "a""b""c""d""e""f""g""h""i""j""k""l""m""n""o""p""q""r",
    "s""t""u""v""w""x""y""z"
    };
    var name = "";
     
    for (var i = 0; i < 5; i++)
    {
    var c = b.Next(025);
    name = name + alphabet[c];
    }
    return name;
     
    }
     
    private void LeOpen1(object sender, RoutedEventArgs e)
    {
    // Configure open file dialog box
    var dlg = new Microsoft.Win32.OpenFileDialog { FileName = "", DefaultExt = ".png", Filter = "PNG-Images (.png)|*.png|JPEG-Images (.jpg)|*.jpg|Bitmapscheisse (.bmp)|*.bmp|All files (*.*)|*.*" };
     
    // Show open file dialog box
    bool? result = dlg.ShowDialog();
     
    // Process open file dialog box results
    if (result == true)
    {
    // Open document
    string filename = dlg.FileName;
    }
    if (!string.IsNullOrEmpty(dlg.FileName))
    {
    file1.Text = dlg.FileName;
    var sr = new StreamReader(dlg.FileName);
    sr.Close();
    var b = new ImageSourceConverter();
    var f = b.ConvertFromString(dlg.FileName);
    image1.Source = (ImageSource) f;
    }
    }
     
    private void LeOpen2(object sender, RoutedEventArgs e)
    {
    // Configure open file dialog box
    var dlg = new Microsoft.Win32.OpenFileDialog { FileName = "", DefaultExt = ".png", Filter = "PNG-Images (.png)|*.png|JPEG-Images (.jpg)|*.jpg|Bitmapscheisse (.bmp)|*.bmp|All files (*.*)|*.*" };
     
    // Show open file dialog box
    bool? result = dlg.ShowDialog();
     
    // Process open file dialog box results
    if (result == true)
    {
    // Open document
    string filename = dlg.FileName;
    }
    if (!string.IsNullOrEmpty(dlg.FileName))
    {
    file2.Text = dlg.FileName;
    var sr = new StreamReader(dlg.FileName);
    sr.Close();
    var b = new ImageSourceConverter();
    var f = b.ConvertFromString(dlg.FileName);
    image2.Source = (ImageSource) f;
    }
    }
    }
    }


Die Open-Funktionen sind hoffentlich selbst erklärend. In file1.Text bzw file2.Text sind die Pfade zu den gewählten Bildern. In der Oberfläche gibt es Radiobuttonse, um die Position vom 2. Bild anzugeben und einen Slider für dessen Skalierung. Bild1 ist der "Hintergrund" und Bild2 wird einfach davor geklatscht.

Nun das schon angesprochene Problem: Es geht immer nur, wenn ich das Bild zwischenspeichere. genName() baut dabei einen Zufallsstring, der als Name fungiert (sonst lässt sich das letzte Bild nicht überschreiben). Darunter wird das zusammengeklatschte Bild gespeichert und wird danach wieder geladen, weil ich es nicht hinbekomme, dass das ganze ohne diesen Schritt funktioniert. Und genau da brauch ich Eure Hilfe

Anbei noch ein Bild:
imageshack.us/photo/...ages/7/schubidu.png/

MfG

Moderiert von user profile iconChristian S.: C#-Tags hinzugefügt
Moderiert von user profile iconChristian S.: Topic aus C# - Die Sprache verschoben am Fr 07.09.2012 um 10:41