teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

8

修正

2017/06/07 14:36

投稿

退会済みユーザー
answer CHANGED
@@ -61,9 +61,8 @@
61
61
  result.Add(imgs[i]);
62
62
 
63
63
  result.Coalesce();
64
- result.RemoveAt(0);
65
64
 
66
- result.Write($"psd\\{String.Format("{0:D6}", i)}.png");
65
+ result[1].Write($"psd\\{String.Format("{0:D6}", i - 1)}.png");
67
66
  }
68
67
 
69
68
  //定期的にGCを入れないとメモリの使用量が大変なことになる。

7

修正

2017/06/07 14:36

投稿

退会済みユーザー
answer CHANGED
@@ -1,6 +1,33 @@
1
1
  Magick.NET様の方で質問をして無事解決しました。
2
2
  dlemstra様本当にありがとうございました。
3
3
 
4
+ dlemstra様に教えていただいたコード。(CodePlexがシャッドダウン?されるそうなので追記。)
5
+ ```C#
6
+ using ImageMagick;
7
+
8
+ namespace psd
9
+ {
10
+ class Program
11
+ {
12
+ static void Main(string[] args)
13
+ {
14
+ using (MagickImageCollection imgs = new MagickImageCollection("psdfile.psd"))
15
+ {
16
+ foreach (MagickImage img in imgs)
17
+ {
18
+ img.GifDisposeMethod = GifDisposeMethod.Background;
19
+ }
20
+
21
+ imgs.Coalesce();
22
+ imgs.RemoveAt(0);
23
+
24
+ imgs.Write("pngfile%04d.png");
25
+ }
26
+ }
27
+ }
28
+ }
29
+
30
+ ```
4
31
  私の質問のURL
5
32
  [How to write Background?](https://magick.codeplex.com/discussions/660412)
6
33
 

6

修正

2017/06/07 14:30

投稿

退会済みユーザー
answer CHANGED
@@ -17,16 +17,11 @@
17
17
  {
18
18
  using (MagickImageCollection imgs = new MagickImageCollection("psd.psd"))
19
19
  {
20
- foreach (MagickImage img in imgs)
21
- {
22
- img.GifDisposeMethod = GifDisposeMethod.Background;
23
- }
24
-
25
20
  int width = imgs[0].Width;
26
21
  int height = imgs[0].Height;
27
22
 
28
23
  //imgs[0]は個人の好みで入れていない。
29
- for(int i = 1; i < imgs.Count; i++)
24
+ for (int i = 1; i < imgs.Count; i++)
30
25
  {
31
26
  //レイヤーの大きさを統一している。for_coalesceを外に置いておくとエラーが発生する。
32
27
  using (MagickImageCollection result = new MagickImageCollection())
@@ -45,7 +40,7 @@
45
40
  }
46
41
 
47
42
  //定期的にGCを入れないとメモリの使用量が大変なことになる。
48
- if(i % 20 == 0)
43
+ if (i % 20 == 0)
49
44
  {
50
45
  GC.Collect();
51
46
  GC.WaitForPendingFinalizers();

5

修正

2017/06/07 14:10

投稿

退会済みユーザー
answer CHANGED
@@ -28,14 +28,14 @@
28
28
  //imgs[0]は個人の好みで入れていない。
29
29
  for(int i = 1; i < imgs.Count; i++)
30
30
  {
31
- //レイヤーの大きさを統一している。for_coalraceを外に置いておくとエラーが発生する。
31
+ //レイヤーの大きさを統一している。for_coalesceを外に置いておくとエラーが発生する。
32
32
  using (MagickImageCollection result = new MagickImageCollection())
33
33
  {
34
34
  MagickColor transparent = new MagickColor(0, 0, 0, 0);
35
- MagickImage for_coalrace = new MagickImage(transparent, width, height);
35
+ MagickImage for_coalesce = new MagickImage(transparent, width, height);
36
- for_coalrace.BackgroundColor = transparent;
36
+ for_coalesce.BackgroundColor = transparent;
37
37
 
38
- result.Add(for_coalrace);
38
+ result.Add(for_coalesce);
39
39
  result.Add(imgs[i]);
40
40
 
41
41
  result.Coalesce();
@@ -56,4 +56,5 @@
56
56
  }
57
57
  }
58
58
  }
59
+
59
60
  ```

4

修正

2017/06/07 14:06

投稿

退会済みユーザー
answer CHANGED
@@ -5,183 +5,55 @@
5
5
  [How to write Background?](https://magick.codeplex.com/discussions/660412)
6
6
 
7
7
  以前のコードだとPSDファイルによってはメモリを大量に使うことになっていたのでそれに対応しました。
8
- このプログラムは公開されている沢山のプログラムを参考にして作っています。
9
-
10
8
  ```C#
11
9
  using System;
12
- using System.Collections.Generic;
13
- using System.Linq;
14
- using System.Text;
15
- using System.Threading.Tasks;
16
- using System.Windows;
17
- using System.Windows.Controls;
18
- using System.Windows.Data;
19
- using System.Windows.Documents;
20
- using System.Windows.Input;
21
- using System.Windows.Media;
22
- using System.Windows.Media.Imaging;
23
- using System.Windows.Navigation;
24
- using System.Windows.Shapes;
25
- using System.IO;
26
10
  using ImageMagick;
27
- using System.Windows.Threading;
28
11
 
29
- namespace PSDConverter4
12
+ namespace psdconvert1
30
13
  {
31
- /// <summary>
32
- /// MainWindow.xaml の相互作用ロジック
33
- /// </summary>
34
- public partial class MainWindow : Window
14
+ class Program
35
15
  {
36
- public MainWindow()
16
+ static void Main(string[] args)
37
17
  {
38
- InitializeComponent();
39
- move_timer = new DispatcherTimer();
40
- move_timer.Interval = TimeSpan.FromSeconds(1);
41
- move_timer.Tick += move_timer_Tick;
42
- }
43
- DispatcherTimer move_timer;
44
- void move_timer_Tick(object sender, EventArgs e)
45
- {
46
- this.Next();
47
- }
48
- private void Clicked_Dialog(object sender, RoutedEventArgs e)
49
- {
50
- Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
18
+ using (MagickImageCollection imgs = new MagickImageCollection("psd.psd"))
51
- dlg.Title = "読み込むファイルを選択してください。";
52
- dlg.DefaultExt = ".psd";
53
- dlg.Filter = "PSDファイル(.psd)|*.psd";
54
-
55
- Nullable<bool> result = dlg.ShowDialog();
56
- if (result == true)
57
19
  {
58
- this.パス.Text = dlg.FileName;
20
+ foreach (MagickImage img in imgs)
21
+ {
22
+ img.GifDisposeMethod = GifDisposeMethod.Background;
59
- }
23
+ }
60
- }
61
- private async void Clicked_Start(object sender, RoutedEventArgs e)
62
- {
63
- path = パス.Text;
64
- if (path.Length > 1 && System.IO.File.Exists(path) == false)
65
- {
66
- MessageBox.Show("『" + System.IO.Path.GetFileNameWithoutExtension(path) + "』は存在しません。", "ERROR", MessageBoxButton.OK, MessageBoxImage.Error);
67
- }
68
- await heavyLogicTaskAsync();
69
- if (success == 1)
70
- {
71
- プログレスバー.Value = max;
72
- MessageBox.Show("作業が完了しました。", "PSD", MessageBoxButton.OK, MessageBoxImage.None);
73
- success = 0;
74
- move_timer.Stop();
75
- }
76
- プログレスバー.Value = 0;
77
- プログレスバー.Maximum = 100;
78
- value_num = 0;
79
- max = 0;
80
- }
81
- private Task heavyLogicTaskAsync()
82
- {
83
- return Task.Run(() => PSD_Convert());
84
- }
85
- public string path = null;
86
- public int max = 0;
87
- public int value_num = 0;
88
- public int success = 0;
89
- private void PSD_Convert()
90
- {
91
24
 
92
- if (path != null)
93
- {
94
- if (path.Length > 1)
25
+ int width = imgs[0].Width;
26
+ int height = imgs[0].Height;
27
+
28
+ //imgs[0]は個人の好みで入れていない。
29
+ for(int i = 1; i < imgs.Count; i++)
95
30
  {
31
+ //レイヤーの大きさを統一している。for_coalraceを外に置いておくとエラーが発生する。
96
- string name = System.IO.Path.GetFileNameWithoutExtension(path);
32
+ using (MagickImageCollection result = new MagickImageCollection())
97
- if (System.IO.File.Exists(path) == true)
98
33
  {
34
+ MagickColor transparent = new MagickColor(0, 0, 0, 0);
99
- MessageBox.Show("『" + name + "』をPNGに変換します。", "PSD", MessageBoxButton.OK, MessageBoxImage.None);
35
+ MagickImage for_coalrace = new MagickImage(transparent, width, height);
36
+ for_coalrace.BackgroundColor = transparent;
100
37
 
101
- try
102
- {
103
- MagickImageCollection imgs = new MagickImageCollection(path);
104
- foreach (MagickImage img in imgs)
105
- {
106
- img.GifDisposeMethod = GifDisposeMethod.Background;
107
- }
108
- int Width = imgs[0].Width;
109
- int Height = imgs[0].Height;
110
- int GC_Count = 0;
111
- if (System.IO.Directory.Exists("converted") == false)
112
- {
113
- System.IO.Directory.CreateDirectory("converted");
114
- }
115
- if (System.IO.Directory.Exists("converted/" + name) == false)
116
- {
117
- System.IO.Directory.CreateDirectory("converted/" + name);
118
- }
119
- max = imgs.Count;
120
- move_timer.Start();
121
- for (int count = 1; count < imgs.Count; ++count)
122
- {
123
- using (MagickImageCollection results = new MagickImageCollection())
124
- {
125
- MagickImage baseimage = new MagickImage(new MagickColor(0, 0, 0, 0), Width, Height);
126
- baseimage.BackgroundColor = new MagickColor(0, 0, 0, 0);
127
- results.Add(baseimage);
38
+ result.Add(for_coalrace);
128
- results.Add(imgs[count]);
39
+ result.Add(imgs[i]);
40
+
129
- results.Coalesce();
41
+ result.Coalesce();
130
- results.RemoveAt(0);
42
+ result.RemoveAt(0);
43
+
131
- string num = String.Format("{0:D6}", count);
44
+ result.Write($"psd\\{String.Format("{0:D6}", i)}.png");
132
- results.Write("converted/" + name + "/" + num + ".png");
133
- results.Dispose();
134
- baseimage.Dispose();
135
- }
136
- GC_Count++;
137
- if (GC_Count >= 20)
138
- {
139
- GC.Collect();
140
- GC.WaitForPendingFinalizers();
141
- GC.Collect();
142
- GC_Count = 0;
143
- }
144
- value_num++;
145
- }
146
- GC.Collect();
147
- GC.WaitForPendingFinalizers();
148
- GC.Collect();
149
- success = 1;
150
- }
151
- catch (ImageMagick.MagickCorruptImageErrorException)
152
- {
153
- MessageBox.Show("『" + name + "』を開けません。", "ERROR", MessageBoxButton.OK, MessageBoxImage.Error);
154
- }
155
45
  }
46
+
47
+ //定期的にGCを入れないとメモリの使用量が大変なことになる。
48
+ if(i % 20 == 0)
49
+ {
50
+ GC.Collect();
51
+ GC.WaitForPendingFinalizers();
52
+ GC.Collect();
53
+ }
156
54
  }
157
55
  }
158
-
159
56
  }
160
- private void Next()
161
- {
162
- プログレスバー.Maximum = max - 1;
163
- プログレスバー.Value = value_num;
164
- }
165
57
  }
166
58
  }
167
- ```
168
- ```XAML
169
- <Window x:Class="PSDConverter4.MainWindow"
170
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
171
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
172
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
173
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
174
- xmlns:local="clr-namespace:PSDConverter4"
175
- mc:Ignorable="d"
176
- Title="PSD" Height="170" Width="250"
177
- Background="Silver"
178
- ResizeMode="NoResize">
179
- <Grid>
180
- <Label x:Name="読み込みファイル名" Content="File Path" HorizontalAlignment="Left" Margin="10,16,0,0" VerticalAlignment="Top" Width="61"/>
181
- <TextBox x:Name="パス" HorizontalAlignment="Left" Height="23" Margin="71,19,0,0" TextWrapping="NoWrap" VerticalAlignment="Top" Width="120"/>
182
- <Button x:Name="ダイアログ" Content="・・・" HorizontalAlignment="Left" Margin="201,19,0,0" VerticalAlignment="Top" Width="22" RenderTransformOrigin="1.205,0.45" Height="23" Click="Clicked_Dialog"/>
183
- <Button x:Name="スタート" Content="START" HorizontalAlignment="Left" Margin="10,92,0,0" VerticalAlignment="Top" Width="213" Height="24" Click="Clicked_Start"/>
184
- <ProgressBar Name="プログレスバー" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="10,58,0,0" Height="16" Width="213" Minimum="0" Maximum="100" IsIndeterminate="False"/>
185
- </Grid>
186
- </Window>
187
59
  ```

3

補足

2017/06/07 12:26

投稿

退会済みユーザー
answer CHANGED
@@ -5,7 +5,7 @@
5
5
  [How to write Background?](https://magick.codeplex.com/discussions/660412)
6
6
 
7
7
  以前のコードだとPSDファイルによってはメモリを大量に使うことになっていたのでそれに対応しました。
8
- このプログラムは公開されている沢山のプログラムを参考にして作られています。
8
+ このプログラムは公開されている沢山のプログラムを参考にして作ています。
9
9
 
10
10
  ```C#
11
11
  using System;

2

補足

2017/02/05 20:52

投稿

退会済みユーザー
answer CHANGED
@@ -5,6 +5,8 @@
5
5
  [How to write Background?](https://magick.codeplex.com/discussions/660412)
6
6
 
7
7
  以前のコードだとPSDファイルによってはメモリを大量に使うことになっていたのでそれに対応しました。
8
+ このプログラムは公開されている沢山のプログラムを参考にして作られています。
9
+
8
10
  ```C#
9
11
  using System;
10
12
  using System.Collections.Generic;

1

補足

2017/02/05 20:51

投稿

退会済みユーザー
answer CHANGED
@@ -2,4 +2,184 @@
2
2
  dlemstra様本当にありがとうございました。
3
3
 
4
4
  私の質問のURL
5
- [How to write Background?](https://magick.codeplex.com/discussions/660412)
5
+ [How to write Background?](https://magick.codeplex.com/discussions/660412)
6
+
7
+ 以前のコードだとPSDファイルによってはメモリを大量に使うことになっていたのでそれに対応しました。
8
+ ```C#
9
+ using System;
10
+ using System.Collections.Generic;
11
+ using System.Linq;
12
+ using System.Text;
13
+ using System.Threading.Tasks;
14
+ using System.Windows;
15
+ using System.Windows.Controls;
16
+ using System.Windows.Data;
17
+ using System.Windows.Documents;
18
+ using System.Windows.Input;
19
+ using System.Windows.Media;
20
+ using System.Windows.Media.Imaging;
21
+ using System.Windows.Navigation;
22
+ using System.Windows.Shapes;
23
+ using System.IO;
24
+ using ImageMagick;
25
+ using System.Windows.Threading;
26
+
27
+ namespace PSDConverter4
28
+ {
29
+ /// <summary>
30
+ /// MainWindow.xaml の相互作用ロジック
31
+ /// </summary>
32
+ public partial class MainWindow : Window
33
+ {
34
+ public MainWindow()
35
+ {
36
+ InitializeComponent();
37
+ move_timer = new DispatcherTimer();
38
+ move_timer.Interval = TimeSpan.FromSeconds(1);
39
+ move_timer.Tick += move_timer_Tick;
40
+ }
41
+ DispatcherTimer move_timer;
42
+ void move_timer_Tick(object sender, EventArgs e)
43
+ {
44
+ this.Next();
45
+ }
46
+ private void Clicked_Dialog(object sender, RoutedEventArgs e)
47
+ {
48
+ Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
49
+ dlg.Title = "読み込むファイルを選択してください。";
50
+ dlg.DefaultExt = ".psd";
51
+ dlg.Filter = "PSDファイル(.psd)|*.psd";
52
+
53
+ Nullable<bool> result = dlg.ShowDialog();
54
+ if (result == true)
55
+ {
56
+ this.パス.Text = dlg.FileName;
57
+ }
58
+ }
59
+ private async void Clicked_Start(object sender, RoutedEventArgs e)
60
+ {
61
+ path = パス.Text;
62
+ if (path.Length > 1 && System.IO.File.Exists(path) == false)
63
+ {
64
+ MessageBox.Show("『" + System.IO.Path.GetFileNameWithoutExtension(path) + "』は存在しません。", "ERROR", MessageBoxButton.OK, MessageBoxImage.Error);
65
+ }
66
+ await heavyLogicTaskAsync();
67
+ if (success == 1)
68
+ {
69
+ プログレスバー.Value = max;
70
+ MessageBox.Show("作業が完了しました。", "PSD", MessageBoxButton.OK, MessageBoxImage.None);
71
+ success = 0;
72
+ move_timer.Stop();
73
+ }
74
+ プログレスバー.Value = 0;
75
+ プログレスバー.Maximum = 100;
76
+ value_num = 0;
77
+ max = 0;
78
+ }
79
+ private Task heavyLogicTaskAsync()
80
+ {
81
+ return Task.Run(() => PSD_Convert());
82
+ }
83
+ public string path = null;
84
+ public int max = 0;
85
+ public int value_num = 0;
86
+ public int success = 0;
87
+ private void PSD_Convert()
88
+ {
89
+
90
+ if (path != null)
91
+ {
92
+ if (path.Length > 1)
93
+ {
94
+ string name = System.IO.Path.GetFileNameWithoutExtension(path);
95
+ if (System.IO.File.Exists(path) == true)
96
+ {
97
+ MessageBox.Show("『" + name + "』をPNGに変換します。", "PSD", MessageBoxButton.OK, MessageBoxImage.None);
98
+
99
+ try
100
+ {
101
+ MagickImageCollection imgs = new MagickImageCollection(path);
102
+ foreach (MagickImage img in imgs)
103
+ {
104
+ img.GifDisposeMethod = GifDisposeMethod.Background;
105
+ }
106
+ int Width = imgs[0].Width;
107
+ int Height = imgs[0].Height;
108
+ int GC_Count = 0;
109
+ if (System.IO.Directory.Exists("converted") == false)
110
+ {
111
+ System.IO.Directory.CreateDirectory("converted");
112
+ }
113
+ if (System.IO.Directory.Exists("converted/" + name) == false)
114
+ {
115
+ System.IO.Directory.CreateDirectory("converted/" + name);
116
+ }
117
+ max = imgs.Count;
118
+ move_timer.Start();
119
+ for (int count = 1; count < imgs.Count; ++count)
120
+ {
121
+ using (MagickImageCollection results = new MagickImageCollection())
122
+ {
123
+ MagickImage baseimage = new MagickImage(new MagickColor(0, 0, 0, 0), Width, Height);
124
+ baseimage.BackgroundColor = new MagickColor(0, 0, 0, 0);
125
+ results.Add(baseimage);
126
+ results.Add(imgs[count]);
127
+ results.Coalesce();
128
+ results.RemoveAt(0);
129
+ string num = String.Format("{0:D6}", count);
130
+ results.Write("converted/" + name + "/" + num + ".png");
131
+ results.Dispose();
132
+ baseimage.Dispose();
133
+ }
134
+ GC_Count++;
135
+ if (GC_Count >= 20)
136
+ {
137
+ GC.Collect();
138
+ GC.WaitForPendingFinalizers();
139
+ GC.Collect();
140
+ GC_Count = 0;
141
+ }
142
+ value_num++;
143
+ }
144
+ GC.Collect();
145
+ GC.WaitForPendingFinalizers();
146
+ GC.Collect();
147
+ success = 1;
148
+ }
149
+ catch (ImageMagick.MagickCorruptImageErrorException)
150
+ {
151
+ MessageBox.Show("『" + name + "』を開けません。", "ERROR", MessageBoxButton.OK, MessageBoxImage.Error);
152
+ }
153
+ }
154
+ }
155
+ }
156
+
157
+ }
158
+ private void Next()
159
+ {
160
+ プログレスバー.Maximum = max - 1;
161
+ プログレスバー.Value = value_num;
162
+ }
163
+ }
164
+ }
165
+ ```
166
+ ```XAML
167
+ <Window x:Class="PSDConverter4.MainWindow"
168
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
169
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
170
+ xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
171
+ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
172
+ xmlns:local="clr-namespace:PSDConverter4"
173
+ mc:Ignorable="d"
174
+ Title="PSD" Height="170" Width="250"
175
+ Background="Silver"
176
+ ResizeMode="NoResize">
177
+ <Grid>
178
+ <Label x:Name="読み込みファイル名" Content="File Path" HorizontalAlignment="Left" Margin="10,16,0,0" VerticalAlignment="Top" Width="61"/>
179
+ <TextBox x:Name="パス" HorizontalAlignment="Left" Height="23" Margin="71,19,0,0" TextWrapping="NoWrap" VerticalAlignment="Top" Width="120"/>
180
+ <Button x:Name="ダイアログ" Content="・・・" HorizontalAlignment="Left" Margin="201,19,0,0" VerticalAlignment="Top" Width="22" RenderTransformOrigin="1.205,0.45" Height="23" Click="Clicked_Dialog"/>
181
+ <Button x:Name="スタート" Content="START" HorizontalAlignment="Left" Margin="10,92,0,0" VerticalAlignment="Top" Width="213" Height="24" Click="Clicked_Start"/>
182
+ <ProgressBar Name="プログレスバー" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="10,58,0,0" Height="16" Width="213" Minimum="0" Maximum="100" IsIndeterminate="False"/>
183
+ </Grid>
184
+ </Window>
185
+ ```