[英]Why i'm getting on form1 Extension method must be defined in a non-generic static class?
錯誤1必須在非泛型靜態類中定義擴展方法
這是form1 top聲明的方式:
public partial class Form1 : Form
然后我將一些變量聲明為靜態:
private static FileInfo newest;
private static Stream mymem;
private static Bitmap ConvertedBmp;
private static Stopwatch sw;
我在form1構造函數中使用此變量:
ConvertedBmp = ConvertTo24(newest.FullName);
mymem = ToStream(ConvertedBmp, ImageFormat.Bmp);
ConvertTo24方法:
private static Bitmap ConvertTo24(string inputFileName)
{
sw = Stopwatch.StartNew();
Bitmap bmpIn = (Bitmap)Bitmap.FromFile(inputFileName);
Bitmap converted = new Bitmap(bmpIn.Width, bmpIn.Height, PixelFormat.Format24bppRgb);
using (Graphics g = Graphics.FromImage(converted))
{
g.PageUnit = GraphicsUnit.Pixel;
g.DrawImageUnscaled(bmpIn, 0, 0);
}
sw.Stop();
return converted;
}
和方法ToStream:
public Stream ToStream(this Image image, ImageFormat formaw)
{
var stream = new System.IO.MemoryStream();
image.Save(stream, formaw);
stream.Position = 0;
return stream;
}
如果我改變任何東西不是靜態的,我在方法ToStream上遇到錯誤:錯誤1擴展方法必須是靜態的
我嘗試做什么靜態獲取Form1上的錯誤,當它不是靜態我在ToStream上收到錯誤所以它必須是靜態方法。
因為您在ToStream
使用this
關鍵字作為第一個參數:
public Stream ToStream(this Image image, ImageFormat formaw)
只允許在擴展方法中使用。 去掉它。
如果你想將它用作擴展方法 (似乎不是這種情況),那么該方法必須位於如下的靜態類中:
public static class MyDrawingExtensions
{
public static Stream ToStream(this Image image, ImageFormat formaw)
{
// ...
}
}
然后你可以用這種方式調用它(也)
mymem = ConvertedBmp.ToStream(ImageFormat.Bmp);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.