質問編集履歴

4

追記

2023/01/07 02:03

投稿

byori
byori

スコア71

test CHANGED
File without changes
test CHANGED
@@ -12,14 +12,7 @@
12
12
  System.IO.__Error.WinIOError(int, string)
13
13
  System.IO.FileStream.Init(string, System.IO.FileMode, System.IO.FileAccess, int, bool, System.IO.FileShare, int, System.IO.FileOptions, Microsoft.Win32.Win32Native.SECURITY_ATTRIBUTES, string, bool, bool, bool)
14
14
  System.IO.FileStream.FileStream(string, System.IO.FileMode, System.IO.FileAccess, System.IO.FileShare)
15
- System.Windows.Media.Imaging.BitmapDecoder.SetupDecoderFromUriOrStream(System.Uri, System.IO.Stream, System.Windows.Media.Imaging.BitmapCacheOption, out System.Guid, out bool, out System.IO.Stream, out System.IO.UnmanagedMemoryStream, out Microsoft.Win32.SafeHandles.SafeFileHandle)
16
- System.Windows.Media.Imaging.BitmapDecoder.CreateFromUriOrStream(System.Uri, System.Uri, System.IO.Stream, System.Windows.Media.Imaging.BitmapCreateOptions, System.Windows.Media.Imaging.BitmapCacheOption, System.Net.Cache.RequestCachePolicy, bool)
17
- System.Windows.Media.Imaging.BitmapImage.FinalizeCreation()
18
- System.Windows.Media.Imaging.BitmapSource.CompleteDelayedCreation()
19
- System.Windows.Media.Imaging.BitmapSource.DpiX.get()
20
- System.Windows.Media.Imaging.BitmapSource.GetWidthInternal()
21
- System.Windows.Media.Imaging.BitmapSource.Width.get()
22
-
15
+      :(文字数制限によりエラーの一部を割愛しました)
23
16
  で、
24
17
  そういうことじゃないとわかっているけど「image」フォルダーを指定位置に作ってみると下記になります
25
18
  System.UnauthorizedAccessException: 'パス 'C:\Users\xxxxx\OneDrive\ドキュメント\Visual Studio 2019\Projects\...\bin\Debug\image' へのアクセスが拒否されました。'
@@ -27,13 +20,7 @@
27
20
  System.IO.__Error.WinIOError(int, string)
28
21
  System.IO.FileStream.Init(string, System.IO.FileMode, System.IO.FileAccess, int, bool, System.IO.FileShare, int, System.IO.FileOptions, Microsoft.Win32.Win32Native.SECURITY_ATTRIBUTES, string, bool, bool, bool)
29
22
  System.IO.FileStream.FileStream(string, System.IO.FileMode, System.IO.FileAccess, System.IO.FileShare)
30
- System.Windows.Media.Imaging.BitmapDecoder.SetupDecoderFromUriOrStream(System.Uri, System.IO.Stream, System.Windows.Media.Imaging.BitmapCacheOption, out System.Guid, out bool, out System.IO.Stream, out System.IO.UnmanagedMemoryStream, out Microsoft.Win32.SafeHandles.SafeFileHandle)
31
- System.Windows.Media.Imaging.BitmapDecoder.CreateFromUriOrStream(System.Uri, System.Uri, System.IO.Stream, System.Windows.Media.Imaging.BitmapCreateOptions, System.Windows.Media.Imaging.BitmapCacheOption, System.Net.Cache.RequestCachePolicy, bool)
32
- System.Windows.Media.Imaging.BitmapImage.FinalizeCreation()
33
- System.Windows.Media.Imaging.BitmapSource.CompleteDelayedCreation()
34
- System.Windows.Media.Imaging.BitmapSource.DpiX.get()
35
- System.Windows.Media.Imaging.BitmapSource.GetWidthInternal()
36
- System.Windows.Media.Imaging.BitmapSource.Width.get()
23
+     :
37
24
  ```
38
25
 
39
26
  このエラーは、Zxing でバーコードを作成すると起きているようです。
@@ -160,4 +147,127 @@
160
147
 
161
148
  上記について、自己レスです。クラスに必要なものを全部追加するよう書いてあったので追加したらエラーが消えました。確認はこれからです。
162
149
 
163
-
150
+ ------------------- 追記②
151
+ 作製した using (var stream = new WrappingStream(new MemoryStream())) クラスでBitmapFrameを作成するとサイズが1になりイメージが表示されません。
152
+ どこがダメなところか教えてください。
153
+
154
+ ```C#
155
+ internal static /*BitmapImage*/BitmapFrame BarcodeCreateForLabel(BarcodeFormat cODE_128, string falco)
156
+ {
157
+ var barcode = new BarcodeWriter
158
+ {
159
+ Format = BarcodeFormat.QR_CODE,
160
+ Options = new ZXing.QrCode.QrCodeEncodingOptions
161
+ {
162
+ ErrorCorrection = ZXing.QrCode.Internal.ErrorCorrectionLevel.M,
163
+ CharacterSet = "ISO-8859-1", // default
164
+ //CharacterSet = "UTF-8", // japanese
165
+ Width = 40,
166
+ Height = 40,
167
+ Margin = 1
168
+ },
169
+ };
170
+
171
+ BitmapFrame barcodebmp;
172
+ using (var bmp = barcode.Write(falco))
173
+ using (var stream = new WrappingStream(new MemoryStream()))
174
+ {
175
+ bmp.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
176
+ barcodebmp = BitmapFrame.Create(stream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
177
+
178
+ /*
179
+ var bitmap = new BitmapImage();
180
+ bitmap.BeginInit();
181
+ bitmap.StreamSource = stream;
182
+ bitmap.CacheOption = BitmapCacheOption.OnLoad;
183
+ bitmap.EndInit();
184
+ // bitmap.Freeze(); ここが有効だと「この Freezable はフリーズできません。」というエラーがでます
185
+ return bitmap;*/
186
+ }
187
+
188
+ return barcodebmp;
189
+ }
190
+
191
+
192
+ // ラッパークラス
193
+ // このコード真ん中辺の「//...(中略)...」より追加しました。エラー回避のため
194
+ public class WrappingStream : Stream
195
+ {
196
+ Stream m_streamBase;
197
+
198
+
199
+ public WrappingStream(Stream streamBase)
200
+ {
201
+ if (streamBase == null)
202
+ {
203
+ throw new ArgumentNullException("streamBase");
204
+ }
205
+ m_streamBase = streamBase; //渡したStreamを内部ストリームとして保持
206
+ }
207
+
208
+ //Streamクラスのメソッドをオーバーライドして、内部ストリームの同じメソッドをそのまま呼ぶだけ
209
+ public override Task<int> ReadAsync(byte[] buffer, int offset, int count, System.Threading.CancellationToken cancellationToken)
210
+ {
211
+ ThrowIfDisposed();
212
+ return m_streamBase.ReadAsync(buffer, offset, count, cancellationToken);
213
+ }
214
+ public new Task<int> ReadAsync(byte[] buffer, int offset, int count)
215
+ {
216
+ ThrowIfDisposed();
217
+ return m_streamBase.ReadAsync(buffer, offset, count);
218
+ }
219
+ //...(中略)...
220
+
221
+ public override long Length { get; }
222
+ public override void SetLength(long value)
223
+ {
224
+ ThrowIfDisposed();
225
+ //return m_streamBase.SetLength(value);
226
+ }
227
+ public override bool CanRead { get; }
228
+ public override int Read(byte[] buffer, int offset, int count)
229
+ {
230
+ ThrowIfDisposed();
231
+ return m_streamBase.Read(buffer, offset, count);
232
+ }
233
+ //public virtual int Read(Span<byte> buffer)
234
+ //{
235
+ // ThrowIfDisposed();
236
+ // return m_streamBase.Read(buffer);
237
+ //}
238
+ public override long Position { get; set; }
239
+ public override void Write(byte[] buffer, int offset, int count)
240
+ {
241
+ ThrowIfDisposed();
242
+ }
243
+ public override bool CanWrite { get; }
244
+ public override long Seek(long offset, System.IO.SeekOrigin origin)
245
+ {
246
+ ThrowIfDisposed();
247
+ return m_streamBase.Seek(offset, origin);
248
+ }
249
+ public override void Flush()
250
+ {
251
+ ThrowIfDisposed();
252
+ }
253
+ public override bool CanSeek { get; }
254
+
255
+
256
+ protected override void Dispose(bool disposing)
257
+ {
258
+ if (disposing)
259
+ {
260
+ m_streamBase.Dispose();
261
+ m_streamBase = null; //disposeしたら内部ストリームをnullにして参照を外す
262
+ }
263
+ base.Dispose(disposing);
264
+ }
265
+ private void ThrowIfDisposed()
266
+ {
267
+ if (m_streamBase == null)
268
+ {
269
+ throw new ObjectDisposedException(GetType().Name);
270
+ }
271
+ }
272
+ }
273
+ ```

3

追記

2023/01/06 07:27

投稿

byori
byori

スコア71

test CHANGED
File without changes
test CHANGED
@@ -158,3 +158,6 @@
158
158
 
159
159
  というエラーがいっぱい出ました。どうしたらいいですか?
160
160
 
161
+ 上記について、自己レスです。クラスに必要なものを全部追加するよう書いてあったので追加したらエラーが消えました。確認はこれからです。
162
+
163
+

2

修正

2023/01/06 06:11

投稿

byori
byori

スコア71

test CHANGED
File without changes
test CHANGED
@@ -149,6 +149,7 @@
149
149
  Windows11 VS2019 C# WPF
150
150
 
151
151
 
152
+ ----------------------- ここから追加事項です
152
153
  ### 試しています
153
154
 
154
155
  教えていただいたサイトから「MemoryStreamをラップしたStreamクラスを作って Dispose時にMemoryStreamの参照を外すと・・・」をコピペしましたが、

1

ご指摘事項を確認中です

2023/01/06 06:10

投稿

byori
byori

スコア71

test CHANGED
File without changes
test CHANGED
@@ -148,3 +148,12 @@
148
148
  ZXing バージョン 0.16.8
149
149
  Windows11 VS2019 C# WPF
150
150
 
151
+
152
+ ### 試しています
153
+
154
+ 教えていただいたサイトから「MemoryStreamをラップしたStreamクラスを作って Dispose時にMemoryStreamの参照を外すと・・・」をコピペしましたが、
155
+
156
+ ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2023-01-06/57c32cd8-84f3-409b-98e4-285ac2c041ce.png)
157
+
158
+ というエラーがいっぱい出ました。どうしたらいいですか?
159
+