Softwareentwicklung
›
Thread
Bitmap Größe ändern
MM
Mike M.
(mikeii)
OP
31.05.2013 17:03
#3185841
Hi,
ich würde gerne in einer Windows 8 App ein Bitmap verkleinern wollen.
Das Problem ist, dass dort alles so "beschnitten" ist und die
vorhandenen Bitmapklassen keine Resizemöglichkeit anbieten(zumindest
finde ich das nicht).
Also wollt eich das auf Byteebene machen.
Ich habe das Bytearray des Originals zur Verfügung, ebenso wie die
Breite.
Ich habe versucht folgenden Code umzustricken, aber es wird nicht so
recht.
Evtl. kann jemand von euch den Fehler finden der beim umstricken
passiert ist?
nWidthFactor und nHeightFactor sind durch den Kehrwert von factor
ersetzt worden, da ich mit gleichem Seitenverhältniss verkleinern
möchte.
Zugriff auf die Colorklasse habe ich auch nicht, daher die einzelnen
Variablen.
1 public void Resize ( int newWidth , int newHeight )
2 {
3 if ( newWidth != 0 && newHeight != 0 )
4 {
5 Bitmap temp = ( Bitmap ) _currentBitmap ;
6 Bitmap bmap = new Bitmap ( newWidth , newHeight , temp . PixelFormat );
7
8 double nWidthFactor = ( double ) temp . Width / ( double ) newWidth ;
9 double nHeightFactor = ( double ) temp . Height / ( double ) newHeight ;
10
11 double fx , fy , nx , ny ;
12 int cx , cy , fr_x , fr_y ;
13 Color color1 = new Color ();
14 Color color2 = new Color ();
15 Color color3 = new Color ();
16 Color color4 = new Color ();
17 byte nRed , nGreen , nBlue ;
18
19 byte bp1 , bp2 ;
20
21 for ( int x = 0 ; x < bmap . Width ; ++ x )
22 {
23 for ( int y = 0 ; y < bmap . Height ; ++ y )
24 {
25
26 fr_x = ( int ) Math . Floor ( x * nWidthFactor );
27 fr_y = ( int ) Math . Floor ( y * nHeightFactor );
28 cx = fr_x + 1 ;
29 if ( cx >= temp . Width ) cx = fr_x ;
30 cy = fr_y + 1 ;
31 if ( cy >= temp . Height ) cy = fr_y ;
32 fx = x * nWidthFactor - fr_x ;
33 fy = y * nHeightFactor - fr_y ;
34 nx = 1 . 0 - fx ;
35 ny = 1 . 0 - fy ;
36
37 color1 = temp . GetPixel ( fr_x , fr_y );
38 color2 = temp . GetPixel ( cx , fr_y );
39 color3 = temp . GetPixel ( fr_x , cy );
40 color4 = temp . GetPixel ( cx , cy );
41
42 // Blue
43 bp1 = ( byte )( nx * color1 . B + fx * color2 . B );
44
45 bp2 = ( byte )( nx * color3 . B + fx * color4 . B );
46
47 nBlue = ( byte )( ny * ( double )( bp1 ) + fy * ( double )( bp2 ));
48
49 // Green
50 bp1 = ( byte )( nx * color1 . G + fx * color2 . G );
51
52 bp2 = ( byte )( nx * color3 . G + fx * color4 . G );
53
54 nGreen = ( byte )( ny * ( double )( bp1 ) + fy * ( double )( bp2 ));
55
56 // Red
57 bp1 = ( byte )( nx * color1 . R + fx * color2 . R );
58
59 bp2 = ( byte )( nx * color3 . R + fx * color4 . R );
60
61 nRed = ( byte )( ny * ( double )( bp1 ) + fy * ( double )( bp2 ));
62
63 bmap . SetPixel ( x , y , System . Drawing . Color . FromArgb
64 ( 255 , nRed , nGreen , nBlue ));
65 }
66 }
67 _currentBitmap = ( Bitmap ) bmap . Clone ();
68 }
69 }
Hier ist der umgestrickte Code, aber ich finde den Fehler nicht... 1 private static int calculatePixelPosition ( int x , int y , int width )
2 {
3
4 return y * 4 * width + x * 4 ;
5 }
6
7
8
9
10 public static byte [] resize ( byte [] src , int originalWidth , int originalHeight , float factor )
11 {
12 //0<Factor<=1
13 byte [] tmp = null ;
14 if ( factor != 0 . 0 )
15 {
16
17
18 int newWidth = ( int )( originalWidth * factor );
19 int newHeight = ( int )( originalHeight * factor );
20
21 tmp = new byte [ newWidth * newHeight * 4 ];
22
23 double fx , fy , nx , ny ;
24 int cx , cy , fr_x , fr_y ;
25
26
27 int c1r ;
28 int c1g ;
29 int c1b ;
30
31 int c2r ;
32 int c2g ;
33 int c2b ;
34
35 int c3r ;
36 int c3g ;
37 int c3b ;
38
39 int c4r ;
40 int c4g ;
41 int c4b ;
42
43
44 byte nRed , nGreen , nBlue ;
45
46 byte bp1 , bp2 ;
47
48
49 for ( int x = 0 ; x < newWidth ; ++ x )
50 {
51 for ( int y = 0 ; y < newHeight ; ++ y )
52 {
53
54 fr_x = ( int ) Math . Floor ( x / factor );
55 fr_y = ( int ) Math . Floor ( y / factor );
56 cx = fr_x + 1 ;
57 if ( cx >= originalWidth ) cx = fr_x ;
58 cy = fr_y + 1 ;
59 if ( cy >= originalHeight ) cy = fr_y ;
60 fx = x / factor - fr_x ;
61 fy = y / factor - fr_y ;
62 nx = 1 . 0 - fx ;
63 ny = 1 . 0 - fy ;
64
65
66
67 c1b = src [ calculatePixelPosition ( fr_x , fr_y , originalWidth )];
68 c1g = src [ calculatePixelPosition ( fr_x , fr_y , originalWidth ) + 1 ];
69 c1r = src [ calculatePixelPosition ( fr_x , fr_y , originalWidth ) + 2 ];
70
71 c2b = src [ calculatePixelPosition ( cx , fr_y , originalWidth )];
72 c2g = src [ calculatePixelPosition ( cx , fr_y , originalWidth ) + 1 ];
73 c2r = src [ calculatePixelPosition ( cx , fr_y , originalWidth ) + 2 ];
74
75 c3b = src [ calculatePixelPosition ( fr_x , cy , originalWidth )];
76 c3g = src [ calculatePixelPosition ( fr_x , cy , originalWidth ) + 1 ];
77 c3r = src [ calculatePixelPosition ( fr_x , cy , originalWidth ) + 2 ];
78
79 c4b = src [ calculatePixelPosition ( cx , cy , originalWidth )];
80 c4g = src [ calculatePixelPosition ( cx , cy , originalWidth ) + 1 ];
81 c4r = src [ calculatePixelPosition ( cx , cy , originalWidth ) + 2 ];
82
83
84
85 // Blue
86 bp1 = ( byte )( nx * c1b + fx * c2b );
87
88 bp2 = ( byte )( nx * c3b + fx * c4b );
89
90 nBlue = ( byte )( ny * ( double )( bp1 ) + fy * ( double )( bp2 ));
91
92 // Green
93 bp1 = ( byte )( nx * c1g + fx * c2g );
94
95 bp2 = ( byte )( nx * c3g + fx * c4g );
96
97 nGreen = ( byte )( ny * ( double )( bp1 ) + fy * ( double )( bp2 ));
98
99 // Red
100 bp1 = ( byte )( nx * c1r + fx * c2r );
101
102 bp2 = ( byte )( nx * c3r + fx * c4r );
103
104 nRed = ( byte )( ny * ( double )( bp1 ) + fy * ( double )( bp2 ));
105
106 tmp [ calculatePixelPosition ( x , y , newWidth )] = ( byte ) nBlue ;
107 tmp [ calculatePixelPosition ( x , y , newWidth ) + 1 ] = ( byte ) nGreen ;
108 tmp [ calculatePixelPosition ( x , y , newWidth ) + 2 ] = ( byte ) nRed ;
109 tmp [ calculatePixelPosition ( x , y , newWidth ) + 3 ] = 255 ;
110
111 }
112 }
113
114 }
115
116 return tmp ;
117
118 }
MM
Mike M.
(mikeii)
OP
31.05.2013 17:07
#3185843
Ups sorry, es funktioniert, Problem war nur, dass dem WriteableBitmap
die neue Höhe und Breite gefehlt hat... Peinlich, sorry...
PI
Peter II
Gast
31.05.2013 17:08
#3185845
MM
Mike M.
(mikeii)
OP
31.05.2013 17:10
#3185850
Hallo, ja C#
Das Problem ist nur, dass in Den Win8Apps alles so abgeschottet ist...
Aber es funktioniert mit dem Code von oben, wie gesagt nur Höhe und
Breite nicht neu zugewiesen gehabt.
PI
Peter II
Gast
31.05.2013 17:12
#3185854
Mike Mike schrieb:
> Das Problem ist nur, dass in Den Win8Apps alles so abgeschottet ist...
kannst du mal etas mehr dazu sagen? .net ist doch über all gleich. Warum
sollte sich die funktionalität unter win8 unterscheiden?
MM
Mike M.
(mikeii)
OP
31.05.2013 17:22
#3185873
Hi,
nicht unter Windows 8 Selber, sondern bei Windows StoreApps.
Dort gibt es Beispielsweise keine Color Klasse und auch nicht die Bitmap
Klasse.
Es gibt ähnliche, sind aber total unbrauchbar. Ich kann bei denen nicht
direkt auf Pixel zugreifen, sondern muss in das Bytearray rein.
Ebenso kann ich aus einer StoreApp keine anderen Programme aufrufen, nur
auf gewisse vorher vom User akzeptierte Geräte zugreifen und auch nur
auf gewisse Pfaden arbeiten.
PI
Peter II
Gast
31.05.2013 17:28
#3185888
Mike Mike schrieb:
> Hi,
> nicht unter Windows 8 Selber, sondern bei Windows StoreApps.
ok - das zeug kenn ich nicht. Unter normalen Win8 sollte eigentlich
alles gehen.
MM
Mike M.
(mikeii)
OP
31.05.2013 17:32
#3185893
Jo das tut es auch, ich bin nur leider gezwungen eine StoreApp zu
nutzen. Das macht nur Ärger :(
Antwort schreiben
Bitte melde dich an, um einen Beitrag zu schreiben.
Noch kein Account?
Die Registrierung ist kostenlos und dauert nur eine Minute.
Jetzt registrieren