Skip to content
This repository was archived by the owner on Dec 18, 2023. It is now read-only.

Commit d9a2397

Browse files
committed
fixed extension class
1 parent c7a7fbb commit d9a2397

File tree

3 files changed

+105
-39
lines changed

3 files changed

+105
-39
lines changed

CookPopularCSharpToolkit/Communal/Extensions/ImageBitmapExtension.cs

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ public static Bitmap ToBitmap(this BitmapSource bitmapSource)
6464
}
6565
}
6666

67-
public static Icon ToIcon(this Bitmap bitmap, System.Drawing.Size size)
67+
public static Icon ToIcon(this Bitmap bitmap, System.Drawing.Size? size)
6868
{
69-
using (Bitmap iconBm = new Bitmap(bitmap, size))
69+
using (Bitmap iconBm = size.HasValue ? new Bitmap(bitmap, size.Value) : new Bitmap(bitmap))
7070
{
7171
using (Icon icon = Icon.FromHandle(iconBm.GetHicon()))
7272
{
@@ -75,30 +75,13 @@ public static Icon ToIcon(this Bitmap bitmap, System.Drawing.Size size)
7575
}
7676
}
7777

78-
public static Icon ToIcon(this ImageSource imageSource)
79-
{
80-
if (imageSource == null) return null;
81-
82-
Uri uri = new Uri(imageSource.ToString());
83-
StreamResourceInfo streamInfo = Application.GetResourceStream(uri);
84-
85-
if (streamInfo == null)
86-
{
87-
string msg = "The supplied image source '{0}' could not be resolved.";
88-
msg = string.Format(msg, imageSource);
89-
throw new ArgumentException(msg);
90-
}
91-
92-
return new Icon(streamInfo.Stream);
93-
}
94-
9578
/// <summary>
9679
/// 转换Image为Icon
9780
/// </summary>
9881
/// <param name="image">要转换为图标的Image对象</param>
9982
/// <param name="nullTonull">当image为null时是否返回null。false则抛空引用异常</param>
10083
/// <exception cref="ArgumentNullException" />
101-
public static Icon ToIcon(this System.Drawing.Image image, bool nullTonull = false)
84+
public static Icon ToIcon(this Image image, bool nullTonull = false)
10285
{
10386
if (image == null)
10487
{
@@ -136,21 +119,35 @@ public static Icon ToIcon(this System.Drawing.Image image, bool nullTonull = fal
136119
}
137120
}
138121

139-
public static void SaveAsIconFile(this Bitmap bitmap, System.Drawing.Size size, string saveFilePath)
122+
public static Icon ToIcon(this ImageSource imageSource)
123+
{
124+
if (imageSource == null) return null;
125+
126+
Uri uri = new Uri(imageSource.ToString());
127+
StreamResourceInfo streamInfo = Application.GetResourceStream(uri);
128+
129+
if (streamInfo == null)
130+
{
131+
string msg = "The supplied image source '{0}' could not be resolved.";
132+
msg = string.Format(msg, imageSource);
133+
throw new ArgumentException(msg);
134+
}
135+
136+
return new Icon(streamInfo.Stream);
137+
}
138+
139+
public static void SaveAsIconFile(this Bitmap bitmap, string saveFilePath, System.Drawing.Size? size)
140140
{
141-
using (Bitmap iconBm = new Bitmap(bitmap, size))
141+
using (Icon icon = size.HasValue ? bitmap.ToIcon(size) : bitmap.ToIcon())
142142
{
143-
using (Icon icon = bitmap.ToIcon(true))
143+
using (Stream stream = new FileStream(saveFilePath, FileMode.Create))
144144
{
145-
using (Stream stream = new FileStream(saveFilePath, FileMode.Create))
146-
{
147-
icon.Save(stream);
148-
}
145+
icon.Save(stream);
149146
}
150147
}
151148
}
152149

153-
public static byte[] ToBytesStreamFromBitmap(int width, int height, int channel, Bitmap img)
150+
public static byte[] ToBytesStreamFromBitmap(this Bitmap img, int width, int height, int channel)
154151
{
155152
byte[] bytes = new byte[width * height * channel];
156153

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Globalization;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using System.Windows;
8+
using System.Windows.Data;
9+
using System.Windows.Markup;
10+
using System.Windows.Media;
11+
12+
13+
/*
14+
* Description:ToBrushOrColorConverters
15+
* Author: Chance.Zheng
16+
* Create Time: 2022-08-28 15:58:08
17+
* .Net Version: 4.6
18+
* CLR Version: 4.0.30319.42000
19+
* Copyright (c) CookCSharp 2020-2022 All Rights Reserved.
20+
*/
21+
namespace CookPopularCSharpToolkit.Windows
22+
{
23+
[MarkupExtensionReturnType(typeof(Color))]
24+
[Localizability(LocalizationCategory.NeverLocalize)]
25+
public class BrushToColorConverter : MarkupExtensionBase, IValueConverter
26+
{
27+
public static readonly BrushToColorConverter Instance = new BrushToColorConverter();
28+
29+
public BrushToColorConverter()
30+
{
31+
}
32+
33+
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
34+
{
35+
var brush = value as SolidColorBrush;
36+
if (brush != null)
37+
return brush.Color;
38+
return null;
39+
}
40+
41+
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
42+
{
43+
throw new NotImplementedException();
44+
}
45+
}
46+
}

CookPopularCSharpToolkit/Windows/Extensions/FramworkElementExtension.cs

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using CookPopularCSharpToolkit.Communal;
22
using System.Drawing;
3+
using System.Drawing.Imaging;
4+
using System.IO;
35
using System.Windows;
46
using System.Windows.Media;
57
using System.Windows.Media.Imaging;
@@ -21,30 +23,42 @@ public static class FramworkElementExtension
2123
/// </summary>
2224
/// <param name="element">元素</param>
2325
/// <param name="fileName">文件路径及文件名</param>
24-
public static void SaveAsPicture(this FrameworkElement element, string fileName)
26+
/// <param name="imageFormat">图片格式</param>
27+
/// <param name="size">保存的图片大小,以pixels为单位</param>
28+
public static void SaveAsPicture(this FrameworkElement element, string fileName, ImageFormat imageFormat, System.Drawing.Size? size = null)
2529
{
2630
var dpiX = DpiHelper.DeviceDpiX;
2731
var dpiY = DpiHelper.DeviceDpiY;
2832

2933
double elementWidth = 0;
3034
double elementHeight = 0;
3135
CheckElementSide(ref elementWidth, ref elementHeight);
32-
3336
int width = (int)(elementWidth * DpiHelper.GetScaleX());
3437
int height = (int)(elementHeight * DpiHelper.GetScaleX());
38+
3539
var bitmapSource = new RenderTargetBitmap(width, height, dpiX, dpiY, PixelFormats.Default);
3640
bitmapSource.Render(element);
3741

38-
//using var ms = new MemoryStream();
39-
//BitmapEncoder encoder = new BmpBitmapEncoder();
40-
//encoder.Frames.Add(BitmapFrame.Create(bitmapSource));
41-
//encoder.Save(ms);
4242
//下面这种方式生成文件很慢
43-
//FileStream fs = File.Open(filePath, FileMode.OpenOrCreate);
43+
//using FileStream fs = File.Open(fileName, FileMode.OpenOrCreate);
44+
//BitmapEncoder encoder = null;
45+
//if (imageFormat == ImageFormat.Jpeg)
46+
// encoder = new JpegBitmapEncoder();
47+
//else if (imageFormat == ImageFormat.Png)
48+
// encoder = new PngBitmapEncoder();
49+
//else if (imageFormat == ImageFormat.Bmp)
50+
// encoder = new BmpBitmapEncoder();
51+
//else if (imageFormat == ImageFormat.Gif)
52+
// encoder = new GifBitmapEncoder();
53+
//else if (imageFormat == ImageFormat.Tiff)
54+
// encoder = new TiffBitmapEncoder();
55+
//else
56+
// throw new InvalidDataException();
57+
//encoder.Frames.Add(BitmapFrame.Create(bitmapSource));
4458
//encoder.Save(fs);
4559

4660
//生成透明背景图片
47-
//using var bitmap = new Bitmap(ms);
61+
//using var bitmap = new Bitmap(fs);
4862
//bitmap.MakeTransparent();
4963
//bitmap.Save(fileName);
5064

@@ -56,19 +70,28 @@ public static void SaveAsPicture(this FrameworkElement element, string fileName)
5670
for (int y = 0; y < height; y++)
5771
for (int x = 0; x < width; x++)
5872
bitmap.SetPixel(x, y, System.Drawing.Color.FromArgb(pixels[y * width + x]));
59-
bitmap.Save(fileName);
73+
74+
if (size.HasValue)
75+
{
76+
using (var newBitmap = new Bitmap(bitmap, size.Value))
77+
{
78+
newBitmap.Save(fileName, ImageFormat.Png);
79+
}
80+
}
81+
else
82+
bitmap.Save(fileName, ImageFormat.Png);
6083
}
6184

6285
void CheckElementSide(ref double elementWidth, ref double elementHeight)
6386
{
64-
if (!double.IsNaN(element.ActualWidth))
87+
if (!double.IsNaN(element.ActualWidth) && element.ActualWidth.CompareTo(0) > 0)
6588
elementWidth = element.ActualWidth;
6689
else if (element.Width.CompareTo(0) > 0)
6790
elementWidth = element.Width;
6891
else
6992
elementWidth = 100;
7093

71-
if (!double.IsNaN(element.ActualHeight))
94+
if (!double.IsNaN(element.ActualHeight) && element.ActualHeight.CompareTo(0) > 0)
7295
elementHeight = element.ActualHeight;
7396
else if (element.Height.CompareTo(0) > 0)
7497
elementHeight = element.Height;

0 commit comments

Comments
 (0)