|
1 | 1 | using DocumentFormat.OpenXml.Packaging; |
2 | 2 | using DocumentFormat.OpenXml.Wordprocessing; |
3 | 3 | using SkiaSharp; |
| 4 | +using System; |
4 | 5 | using System.Globalization; |
5 | 6 | using System.Linq; |
6 | 7 | using System.Xml.Linq; |
@@ -362,129 +363,14 @@ internal static bool TryGetPixelSize(byte[] imageBytes, out int width, out int h |
362 | 363 | return true; |
363 | 364 | } |
364 | 365 | } |
365 | | - catch |
366 | | - { |
367 | | - // ignore and fall back to header-based detection |
368 | | - } |
369 | | - |
370 | | - if (TryReadPngDimensions(imageBytes, out width, out height)) |
371 | | - { |
372 | | - return true; |
373 | | - } |
374 | | - |
375 | | - if (TryReadJpegDimensions(imageBytes, out width, out height)) |
376 | | - { |
377 | | - return true; |
378 | | - } |
379 | | - |
380 | | - if (TryReadGifDimensions(imageBytes, out width, out height)) |
381 | | - { |
382 | | - return true; |
383 | | - } |
384 | | - |
385 | | - errorMessage = "Image: Unable to determine image dimensions."; |
386 | | - return false; |
387 | | - } |
388 | | - |
389 | | - /// <summary> |
390 | | - /// Reads PNG image dimensions from the file header. |
391 | | - /// </summary> |
392 | | - private static bool TryReadPngDimensions(byte[] bytes, out int width, out int height) |
393 | | - { |
394 | | - width = 0; |
395 | | - height = 0; |
396 | | - if (bytes.Length < 24) |
397 | | - { |
398 | | - return false; |
399 | | - } |
400 | | - |
401 | | - var signature = new byte[] { 137, 80, 78, 71, 13, 10, 26, 10 }; |
402 | | - for (var i = 0; i < signature.Length; i++) |
403 | | - { |
404 | | - if (bytes[i] != signature[i]) |
405 | | - { |
406 | | - return false; |
407 | | - } |
408 | | - } |
409 | | - |
410 | | - if (bytes[12] != 0x49 || bytes[13] != 0x48 || bytes[14] != 0x44 || bytes[15] != 0x52) |
411 | | - { |
412 | | - return false; |
413 | | - } |
414 | | - |
415 | | - width = (bytes[16] << 24) | (bytes[17] << 16) | (bytes[18] << 8) | bytes[19]; |
416 | | - height = (bytes[20] << 24) | (bytes[21] << 16) | (bytes[22] << 8) | bytes[23]; |
417 | | - return width > 0 && height > 0; |
418 | | - } |
419 | | - |
420 | | - /// <summary> |
421 | | - /// Reads JPEG image dimensions from the file header. |
422 | | - /// </summary> |
423 | | - private static bool TryReadJpegDimensions(byte[] bytes, out int width, out int height) |
424 | | - { |
425 | | - width = 0; |
426 | | - height = 0; |
427 | | - if (bytes.Length < 4 || bytes[0] != 0xFF || bytes[1] != 0xD8) |
| 366 | + catch (Exception excepiton) |
428 | 367 | { |
| 368 | + errorMessage = excepiton.Message; |
429 | 369 | return false; |
430 | 370 | } |
431 | 371 |
|
432 | | - var index = 2; |
433 | | - while (index + 9 < bytes.Length) |
434 | | - { |
435 | | - if (bytes[index] != 0xFF) |
436 | | - { |
437 | | - index++; |
438 | | - continue; |
439 | | - } |
440 | | - |
441 | | - var marker = bytes[index + 1]; |
442 | | - index += 2; |
443 | | - |
444 | | - if (marker == 0xD8 || marker == 0xD9) |
445 | | - { |
446 | | - continue; |
447 | | - } |
448 | | - |
449 | | - if (index + 2 > bytes.Length) |
450 | | - { |
451 | | - break; |
452 | | - } |
453 | | - |
454 | | - var length = (bytes[index] << 8) + bytes[index + 1]; |
455 | | - if (length < 2 || index + length > bytes.Length) |
456 | | - { |
457 | | - break; |
458 | | - } |
459 | | - |
460 | | - if (marker >= 0xC0 && marker <= 0xC3) |
461 | | - { |
462 | | - height = (bytes[index + 3] << 8) + bytes[index + 4]; |
463 | | - width = (bytes[index + 5] << 8) + bytes[index + 6]; |
464 | | - return width > 0 && height > 0; |
465 | | - } |
466 | | - |
467 | | - index += length; |
468 | | - } |
469 | | - |
| 372 | + errorMessage = "Image: Unable to determine image dimensions."; |
470 | 373 | return false; |
471 | 374 | } |
472 | | - |
473 | | - /// <summary> |
474 | | - /// Reads GIF image dimensions from the file header. |
475 | | - /// </summary> |
476 | | - private static bool TryReadGifDimensions(byte[] bytes, out int width, out int height) |
477 | | - { |
478 | | - width = 0; |
479 | | - height = 0; |
480 | | - if (bytes.Length < 10 || bytes[0] != 'G' || bytes[1] != 'I' || bytes[2] != 'F') |
481 | | - { |
482 | | - return false; |
483 | | - } |
484 | | - |
485 | | - width = bytes[6] | (bytes[7] << 8); |
486 | | - height = bytes[8] | (bytes[9] << 8); |
487 | | - return width > 0 && height > 0; |
488 | | - } |
489 | 375 | } |
490 | 376 | } |
0 commit comments