回答編集履歴

1

見直しキャンペーン中

2023/07/28 16:35

投稿

TN8001
TN8001

スコア9884

test CHANGED
@@ -1,231 +1,116 @@
1
1
  解決されているようですが、`System.Drawing`を参照したくないのでWPFのみバージョンです。
2
-
3
-
4
2
 
5
3
  > "MSP 明朝"
6
4
 
7
-
8
-
9
5
  "MS P明朝"じゃないですかね?
10
6
 
11
-
12
-
13
- ```xaml
7
+ ```xml
14
-
15
8
  <Window
16
-
17
9
  x:Class="Questions354935.MainWindow"
18
-
19
10
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
20
-
21
11
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
22
-
23
12
  Width="800"
24
-
25
13
  Height="450">
26
-
27
14
  <Grid>
28
-
29
15
  <Canvas Name="canvas1" Background="LightGray" />
30
-
31
16
  <TextBlock
32
-
33
17
  FontFamily="MS P明朝"
34
-
35
18
  Foreground="Red"
36
-
37
19
  Text="XXXXX - TextBlock" FontSize="40pt" />
38
-
39
20
  </Grid>
40
-
41
21
  </Window>
42
-
43
22
  ```
44
23
 
45
-
46
-
47
- ```C#
24
+ ```cs
48
-
49
25
  using System;
50
-
51
26
  using System.Globalization;
52
-
53
27
  using System.Windows;
54
-
55
28
  using System.Windows.Controls;
56
-
57
29
  using System.Windows.Interop;
58
-
59
30
  using System.Windows.Media;
60
-
61
31
  using System.Windows.Media.Imaging;
62
32
 
63
-
64
-
65
33
  namespace Questions354935
66
-
67
34
  {
68
-
69
35
  public partial class MainWindow : Window
70
-
71
36
  {
72
-
73
37
  public MainWindow()
74
-
75
38
  {
76
-
77
39
  InitializeComponent();
78
-
79
40
  TestImage();
80
-
81
41
  TestImage2();
82
-
83
42
  }
84
43
 
85
-
86
-
87
44
  private void TestImage()
88
-
89
45
  {
90
-
91
46
  //var font = new System.Drawing.Font("MSP 明朝", 40);
92
-
93
47
  var font = new System.Drawing.Font("MS P明朝", 40);
94
48
 
95
-
96
-
97
49
  using (var bmp = new System.Drawing.Bitmap(800, 200))
98
-
99
50
  {
100
-
101
51
  bmp.SetResolution(96, 96);
102
-
103
52
  using (var g = System.Drawing.Graphics.FromImage(bmp))
104
-
105
53
  {
106
-
107
54
  g.DrawImage(bmp, 0, 0, bmp.Width, bmp.Height);
108
-
109
55
  using (var b = new System.Drawing.SolidBrush(System.Drawing.Color.Red))
110
-
111
56
  {
112
-
113
57
  g.Clear(System.Drawing.Color.Transparent/*FromArgb(212, 212, 212).*/);
114
-
115
58
  g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
116
-
117
59
  g.DrawString("XXXXX - System.Drawing", font, b, 0, 0); // 10, 22);
118
-
119
60
  }
120
-
121
61
  }
122
62
 
123
-
124
-
125
63
  var hBmp = bmp.GetHbitmap();
126
-
127
64
  var bm = Imaging.CreateBitmapSourceFromHBitmap(
128
-
129
65
  hBmp,
130
-
131
66
  IntPtr.Zero,
132
-
133
67
  Int32Rect.Empty,
134
-
135
68
  BitmapSizeOptions.FromEmptyOptions());
136
69
 
137
-
138
-
139
70
  var img = new Image
140
-
141
71
  {
142
-
143
72
  Width = 800,
144
-
145
73
  Height = 200,
146
-
147
74
  Source = bm,
148
-
149
75
  };
150
-
151
76
  Canvas.SetTop(img, 100);
152
-
153
77
  //Canvas.SetLeft(img, 50);
154
78
 
155
-
156
-
157
79
  canvas1.Children.Add(img);
158
-
159
80
  }
160
-
161
81
  }
162
82
 
163
-
164
-
165
83
  private void TestImage2()
166
-
167
84
  {
168
-
169
85
  var text = new FormattedText("XXXXX - DrawingVisual",
170
-
171
86
  CultureInfo.CurrentCulture,
172
-
173
87
  FlowDirection.LeftToRight,
174
-
175
88
  new Typeface("MS P明朝"),
176
-
177
89
  40 * (96.0 / 72.0),
178
-
179
90
  Brushes.Red, 96);
180
91
 
181
-
182
-
183
92
  var drawingVisual = new DrawingVisual();
184
-
185
93
  var drawingContext = drawingVisual.RenderOpen();
186
-
187
94
  drawingContext.DrawText(text, new Point());
188
-
189
95
  drawingContext.Close();
190
96
 
191
-
192
-
193
97
  var bmp = new RenderTargetBitmap(800, 200, 96, 96, PixelFormats.Pbgra32);
194
-
195
98
  bmp.Render(drawingVisual);
196
99
 
197
-
198
-
199
100
  var img = new Image
200
-
201
101
  {
202
-
203
102
  Width = 800,
204
-
205
103
  Height = 200,
206
-
207
104
  Source = bmp,
208
-
209
105
  };
210
-
211
106
  Canvas.SetTop(img, 200);
212
-
213
107
  //Canvas.SetLeft(img, 50);
214
108
 
215
-
216
-
217
109
  canvas1.Children.Add(img);
218
-
219
110
  }
220
-
221
111
  }
222
-
223
112
  }
224
-
225
113
  ```
226
-
227
114
  ![アプリ画像](47303e42ed17c089ee6c352816a36832.png)
228
115
 
229
-
230
-
231
116
  微妙に違うのですが、なんでかはわかりません^^;