问题描述
任何人都知道什么这个问题?
我得到这个警告字段XXX永远不会分配给,永远对私有静态它的默认值空
量化的 quantit
我不知道该做些什么来解决,因为当我尝试使用 quantit.Quantize()
调试说:对象引用不设置到对象的实例。并点 AU = quantit .Quantize();
中的代码:
公共类量化:System.Windows.Forms.Form中
{
私有静态量化quantit;
私人按钮btnLoad;
私人图片框imgPhoto;
公共量化()
{
btnLoad =新按钮();
btnLoad.Text =&放大器;装载;
btnLoad.Left = 10;
btnLoad.Top = 10;
btnLoad.Click + =新System.EventHandler(this.OnLoadClick);
imgPhoto =新的PictureBox();
imgPhoto.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
imgPhoto.Width = this.Width / 2;
imgPhoto.Height = this.Height / 2;
imgPhoto.Left =(this.Width - imgPhoto.Width)/ 2;
imgPhoto.Top =(this.Height - imgPhoto.Height)/ 2;
imgPhoto.SizeMode = PictureBoxSizeMode.StretchImage;
this.Controls.Add(btnLoad);
this.Controls.Add(imgPhoto);
}
保护无效OnLoadClick(对象发件人,发送System.EventArgs)
{
打开文件对话框DLG =新的OpenFileDialog();
dlg.Title =开放图像;
dlg.Filter =JPG文件(* .JPG)| * .JPG |所有文件| *。*(*。*)。
如果(dlg.ShowDialog()== DialogResult.OK)
{
位图金;
//图像BMP = Image.FromFile(D:\\Documents和Settings\\kiosk.suprisul\\My Documents\\foto1.jpg);
AU = quantit.Quantize();
imgPhoto.Image =金;
//imgPhoto.Image = BMP;
//imgPhoto.Image =金;
//新位图(dlg.OpenFile());
}
dlg.Dispose();
}
[STAThread]
静态无效的主要(字串[] args)
{
// BMP图像;
// BMP = Image.FromFile(teste.jpg);
// PaintEventArgs的Ë;
//teste2.Quantize(bmp);
Application.Run(新量化());
/ *
的System.Console.WriteLine(你好,世界!);
System.Console.ReadLine(); * /
}
}
类:
命名空间ImageManipulation
{
公共不安全的抽象类量化
{
///<总结>
///构建量化
///< /总结>
///< PARAM NAME =SINGLEPASS> /参数>如果为true,量化只能通过源像素曾经与LT需要循环;
///<&言论GT;
///如果您构建这个类与SINGLEPASS一个真正的价值,那么代码,量化的图片时,
///只称之为QuantizeImage功能。如果需要两遍,代码将称之为InitialQuantizeImage'
///然后'QuantizeImage。
///< /言论>
公共量化(布尔SINGLEPASS)
{
_singlePass = SINGLEPASS;
}
///<总结>
///量子化的图像,并返回结果输出位
///< /总结>
///< PARAM NAME =源> /参数>到&量化所述的形象;
///<返回>在图像℃的量化形式; /回报>
公共位图量化()//图片来源)
{
图片来源= Image.FromFile(C:\\Users\\crashboy\\Downloads\ \image009.jpg);
//获取源图像
INT高度= source.Height的大小;
INT宽度= source.Width;
//和建设从这些尺寸
矩形范围=新的Rectangle(0,0,宽度,高度)的矩形;
//首先采取的图像
位图副本=新位图(宽度,高度,PixelFormat.Format32bppArgb)一个32bpp的复印件;
//并构造一个8bpp版本
位图输出=新位图(宽度,高度,PixelFormat.Format8bppIndexed);
//使用现在锁定位图到内存
(图形G = Graphics.FromImage(复印件))
{
g.PageUnit = GraphicsUnit.Pixel;
//绘制源图像到副本位图,
//这将影响不断扩大的为宜。
g.DrawImage(来源,边界);
}
//定义的指针位图数据
的BitmapData sourceData =无效;
试
{
//获取源图像位并锁入内存
sourceData = copy.LockBits(边界,ImageLockMode.ReadOnly,PixelFormat.Format32bppArgb);
//调用FirstPass功能如果没有一个单一的通算法。
//对于类似八叉树量化,这将通过
//所有的图像像素运行,建立一个数据结构,并创建一个调色板。
如果
FirstPass(sourceData,宽,高)(_singlePass!);
//然后设置在输出位图的调色板。我路过当前调色板
//因为没有办法来构造一个新的空调色板。
output.Palette = this.GetPalette(output.Palette);
//然后调用它实际上执行转换
SecondPass(sourceData,输出,宽度,高度,边界)第二遍;
}
终于
{
//确保位是解锁
copy.UnlockBits(sourceData);
}
//最后但并非最不重要的,返回输出位
返回输出;
}
///<总结>
///执行通过在图像
中的像素所述第一遍///&; /总结>
///< PARAM NAME =sourceData>将源数据与LT; /参数>
///< PARAM NAME =宽>在图像和LT的像素宽度; /参数>
///< PARAM NAME =高度>在图像和LT的像素高度; /参数>
受保护的虚拟无效FirstPass(的BitmapData sourceData,诠释的宽度,高度INT)
{
//定义源数据的指针。源行是
字节//除了保持步幅值更容易(因为这是按字节)
字节* pSourceRow =(BYTE *)sourceData.Scan0.ToPointer();
的Int32 * pSourcePixel;
//循环通过每个行
表示(中间体行= 0;行与下;高度;行++)
{
//源像素设置为所述第一在此行
pSourcePixel =(的Int32 *)pSourceRow像素;
//并遍历每一列
为(INT COL = 0;山坳<宽度;山坳++,pSourcePixel ++)
//现在我有像素,调用FirstPassQuantize功能...
InitialQuantizePixel((Color32 *)pSourcePixel);
//添加箭步源行
pSourceRow + = sourceData.Stride;
}
}
///<总结>
///执行通过位图
LT第二遍///&; /总结>
///< PARAM NAME =sourceData>在源位图,锁定到内存< /参数>
///< PARAM NAME =输出>在输出位图< /参数>
///< PARAM NAME =宽>在图像和LT的像素宽度; /参数>
///< PARAM NAME =高度>在图像和LT的像素高度; /参数>
///< PARAM NAME =界限>在边框和LT; /参数>
受保护的虚拟无效SecondPass(的BitmapData sourceData,位图输出,诠释的宽度,高度INT,矩形范围)
{
的BitmapData outputData = NULL;
试
{
//锁定输出位图到内存
outputData = output.LockBits(边界,ImageLockMode.WriteOnly,PixelFormat.Format8bppIndexed);
//定义源数据的指针。源行是
字节//除了保持步幅值更容易(因为这是按字节)
字节* pSourceRow =(BYTE *)sourceData.Scan0.ToPointer();
的Int32 * pSourcePixel =(*的Int32)pSourceRow;
的Int32 * pPreviousPixel = pSourcePixel;
//现在定义目标数据指针
字节* pDestinationRow =(BYTE *)outputData.Scan0.ToPointer();
字节* pDestinationPixel = pDestinationRow;
//并转换的第一个像素,使我有值进入循环
字节pixelValue = QuantizePixel((Color32 *)pSourcePixel);
//分配第一像素
的值* pDestinationPixel = pixelValue;
//循环通过每个行
表示(中间体行= 0;行与下;高度;行++)
{
//源像素设置为所述第一在此行
pSourcePixel =(的Int32 *)pSourceRow像素;
//并设置目标像素指针在行$ B $第一像素B pDestinationPixel = pDestinationRow;
//循环通过该扫描线
为每个像素(INT COL = 0;山坳<宽度;山坳++,pSourcePixel ++,pDestinationPixel ++)
{
/ /检查,如果这是一样的最后一个像素。如果是这样使用该值
//,而不是重新计算它。这是一种廉价的优化。如果(!* pPreviousPixel = * pSourcePixel)
{
//量化像素
pixelValue = QuantizePixel((Color32 *)pSourcePixel)
;
//并设置在先指针
pPreviousPixel = pSourcePixel;
}
//并设置输出
像素* pDestinationPixel = pixelValue;
}
//添加箭步源行
pSourceRow + = sourceData.Stride;
//而到了目的地的行
pDestinationRow + = outputData.Stride;
}
}
终于
{
//确保我解锁输出位
output.UnlockBits(outputData);
}
}
///<总结>
///重写此处理算法中的
的第一遍所述像素///&; /总结>
///&下; PARAM NAME =像素>到量化像素与下/参数>
///<&言论GT;
///此功能只需要您的量化算法需要两次传球,
///如八叉树量化覆盖。
///< /言论>
受保护的虚拟无效InitialQuantizePixel(Color32 *像素)
{
}
///<总结>
///重写此处理算法中的
的第二次LT像素///&; /总结>
///&下; PARAM NAME =像素>到量化像素与下/参数>
///<返回>将量化值< /回报>
保护抽象的字节QuantizePixel(Color32 *像素);
///<总结>
///检索量化图像
///<调色板; /总结>
///< PARAM NAME =原始>的任何旧的调色板,这是overrwritten< /参数>
///<返回>在新的调色板< /回报>
保护抽象ColorPalette GetPalette进行(ColorPalette原件);
///<总结>
///标志用于指示是否需要单次或两个通行证量化。
///< /总结>
私人布尔_singlePass;
///<总结>
///结构体,它定义了32位颜色
///< /总结>
///<&言论GT;
///这个结构被用来从每个像素的图像
///在存储器中的32位的读取数据,并以这种方式被命令,因为这是
中的方式///在内存
///<数据奠定了; /言论>
[StructLayout(LayoutKind.Explicit)
公共结构Color32
{
///<总结>
///举行的颜色
///<的蓝色成分; /总结>
[FieldOffset(0)]
公共字节蓝色;
///<总结>
///举行的颜色
///<的绿色成分; /总结>
[FieldOffset(1)]
公共字节绿色;
///<总结>
///举行的颜色
///<的红色成分; /总结>
[FieldOffset(2)]
公共字节红;
///<总结>
///举行的颜色
///<的alpha成分; /总结>
[FieldOffset(3)]
公共字节阿尔法;
///<总结>
///允许的color32被视为一个Int32
///< /总结>
[FieldOffset(0)]
公众诠释ARGB;
///<总结>
///返回颜色这个Color32对象
///< /总结>
众彩彩
{
{返回Color.FromArgb(Alpha,红,绿,蓝); }
}
}
}
}
编译器警告您 quantit
从未初始化,而且将永远是空
。
您或许应该与从 ImageManipulation.Quantizer
(你不能在一个类的实例初始化实例量化
本身,因为它是一个类):
私有静态量化quantit =新QuantizerImplementation();
Anyone know whats this problem?
I got this warning Field xxx is never assigned to, and will always have its default value null on private static Quantizer quantit;
I dont know what to do to fix, cause when I try to use quantit.Quantize()
debug says: "Object reference not set to an instance of an object." and point to au = quantit.Quantize();
The code:
public class Quantization : System.Windows.Forms.Form
{
private static Quantizer quantit;
private Button btnLoad;
private PictureBox imgPhoto;
public Quantization()
{
btnLoad = new Button();
btnLoad.Text = "&Load";
btnLoad.Left = 10;
btnLoad.Top = 10;
btnLoad.Click += new System.EventHandler(this.OnLoadClick);
imgPhoto = new PictureBox();
imgPhoto.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
imgPhoto.Width = this.Width / 2;
imgPhoto.Height = this.Height / 2;
imgPhoto.Left = (this.Width - imgPhoto.Width) / 2;
imgPhoto.Top = (this.Height - imgPhoto.Height) / 2;
imgPhoto.SizeMode = PictureBoxSizeMode.StretchImage;
this.Controls.Add(btnLoad);
this.Controls.Add(imgPhoto);
}
protected void OnLoadClick(object sender, System.EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Title = "Open Image";
dlg.Filter = "jpg files (*.jpg)|*.jpg|All files (*.*)|*.*" ;
if (dlg.ShowDialog() == DialogResult.OK)
{
Bitmap au;
//Image bmp = Image.FromFile("D:\\Documents and Settings\\kiosk.suprisul\\My Documents\\foto1.jpg");
au = quantit.Quantize();
imgPhoto.Image = au;
//imgPhoto.Image = bmp;
//imgPhoto.Image = au;
//new Bitmap(dlg.OpenFile());
}
dlg.Dispose();
}
[STAThread]
static void Main(string[] args)
{
//Image bmp;
//bmp = Image.FromFile("teste.jpg");
//PaintEventArgs e;
//teste2.Quantize(bmp);
Application.Run(new Quantization());
/*
System.Console.WriteLine("Hello, World!");
System.Console.ReadLine();*/
}
}
The class:
namespace ImageManipulation
{
public unsafe abstract class Quantizer
{
/// <summary>
/// Construct the quantizer
/// </summary>
/// <param name="singlePass">If true, the quantization only needs to loop through the source pixels once</param>
/// <remarks>
/// If you construct this class with a true value for singlePass, then the code will, when quantizing your image,
/// only call the 'QuantizeImage' function. If two passes are required, the code will call 'InitialQuantizeImage'
/// and then 'QuantizeImage'.
/// </remarks>
public Quantizer(bool singlePass)
{
_singlePass = singlePass;
}
/// <summary>
/// Quantize an image and return the resulting output bitmap
/// </summary>
/// <param name="source">The image to quantize</param>
/// <returns>A quantized version of the image</returns>
public Bitmap Quantize()//Image source)
{
Image source = Image.FromFile("C:\\Users\\crashboy\\Downloads\\image009.jpg");
// Get the size of the source image
int height = source.Height;
int width = source.Width;
// And construct a rectangle from these dimensions
Rectangle bounds = new Rectangle(0, 0, width, height);
// First off take a 32bpp copy of the image
Bitmap copy = new Bitmap(width, height, PixelFormat.Format32bppArgb);
// And construct an 8bpp version
Bitmap output = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
// Now lock the bitmap into memory
using (Graphics g = Graphics.FromImage(copy))
{
g.PageUnit = GraphicsUnit.Pixel;
// Draw the source image onto the copy bitmap,
// which will effect a widening as appropriate.
g.DrawImage(source, bounds);
}
// Define a pointer to the bitmap data
BitmapData sourceData = null;
try
{
// Get the source image bits and lock into memory
sourceData = copy.LockBits(bounds, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
// Call the FirstPass function if not a single pass algorithm.
// For something like an octree quantizer, this will run through
// all image pixels, build a data structure, and create a palette.
if (!_singlePass)
FirstPass(sourceData, width, height);
// Then set the color palette on the output bitmap. I'm passing in the current palette
// as there's no way to construct a new, empty palette.
output.Palette = this.GetPalette(output.Palette);
// Then call the second pass which actually does the conversion
SecondPass(sourceData, output, width, height, bounds);
}
finally
{
// Ensure that the bits are unlocked
copy.UnlockBits(sourceData);
}
// Last but not least, return the output bitmap
return output;
}
/// <summary>
/// Execute the first pass through the pixels in the image
/// </summary>
/// <param name="sourceData">The source data</param>
/// <param name="width">The width in pixels of the image</param>
/// <param name="height">The height in pixels of the image</param>
protected virtual void FirstPass(BitmapData sourceData, int width, int height)
{
// Define the source data pointers. The source row is a byte to
// keep addition of the stride value easier (as this is in bytes)
byte* pSourceRow = (byte*)sourceData.Scan0.ToPointer();
Int32* pSourcePixel;
// Loop through each row
for (int row = 0; row < height; row++)
{
// Set the source pixel to the first pixel in this row
pSourcePixel = (Int32*)pSourceRow;
// And loop through each column
for (int col = 0; col < width; col++, pSourcePixel++)
// Now I have the pixel, call the FirstPassQuantize function...
InitialQuantizePixel((Color32*)pSourcePixel);
// Add the stride to the source row
pSourceRow += sourceData.Stride;
}
}
/// <summary>
/// Execute a second pass through the bitmap
/// </summary>
/// <param name="sourceData">The source bitmap, locked into memory</param>
/// <param name="output">The output bitmap</param>
/// <param name="width">The width in pixels of the image</param>
/// <param name="height">The height in pixels of the image</param>
/// <param name="bounds">The bounding rectangle</param>
protected virtual void SecondPass(BitmapData sourceData, Bitmap output, int width, int height, Rectangle bounds)
{
BitmapData outputData = null;
try
{
// Lock the output bitmap into memory
outputData = output.LockBits(bounds, ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
// Define the source data pointers. The source row is a byte to
// keep addition of the stride value easier (as this is in bytes)
byte* pSourceRow = (byte*)sourceData.Scan0.ToPointer();
Int32* pSourcePixel = (Int32*)pSourceRow;
Int32* pPreviousPixel = pSourcePixel;
// Now define the destination data pointers
byte* pDestinationRow = (byte*)outputData.Scan0.ToPointer();
byte* pDestinationPixel = pDestinationRow;
// And convert the first pixel, so that I have values going into the loop
byte pixelValue = QuantizePixel((Color32*)pSourcePixel);
// Assign the value of the first pixel
*pDestinationPixel = pixelValue;
// Loop through each row
for (int row = 0; row < height; row++)
{
// Set the source pixel to the first pixel in this row
pSourcePixel = (Int32*)pSourceRow;
// And set the destination pixel pointer to the first pixel in the row
pDestinationPixel = pDestinationRow;
// Loop through each pixel on this scan line
for (int col = 0; col < width; col++, pSourcePixel++, pDestinationPixel++)
{
// Check if this is the same as the last pixel. If so use that value
// rather than calculating it again. This is an inexpensive optimisation.
if (*pPreviousPixel != *pSourcePixel)
{
// Quantize the pixel
pixelValue = QuantizePixel((Color32*)pSourcePixel);
// And setup the previous pointer
pPreviousPixel = pSourcePixel;
}
// And set the pixel in the output
*pDestinationPixel = pixelValue;
}
// Add the stride to the source row
pSourceRow += sourceData.Stride;
// And to the destination row
pDestinationRow += outputData.Stride;
}
}
finally
{
// Ensure that I unlock the output bits
output.UnlockBits(outputData);
}
}
/// <summary>
/// Override this to process the pixel in the first pass of the algorithm
/// </summary>
/// <param name="pixel">The pixel to quantize</param>
/// <remarks>
/// This function need only be overridden if your quantize algorithm needs two passes,
/// such as an Octree quantizer.
/// </remarks>
protected virtual void InitialQuantizePixel(Color32* pixel)
{
}
/// <summary>
/// Override this to process the pixel in the second pass of the algorithm
/// </summary>
/// <param name="pixel">The pixel to quantize</param>
/// <returns>The quantized value</returns>
protected abstract byte QuantizePixel(Color32* pixel);
/// <summary>
/// Retrieve the palette for the quantized image
/// </summary>
/// <param name="original">Any old palette, this is overrwritten</param>
/// <returns>The new color palette</returns>
protected abstract ColorPalette GetPalette(ColorPalette original);
/// <summary>
/// Flag used to indicate whether a single pass or two passes are needed for quantization.
/// </summary>
private bool _singlePass;
/// <summary>
/// Struct that defines a 32 bpp colour
/// </summary>
/// <remarks>
/// This struct is used to read data from a 32 bits per pixel image
/// in memory, and is ordered in this manner as this is the way that
/// the data is layed out in memory
/// </remarks>
[StructLayout(LayoutKind.Explicit)]
public struct Color32
{
/// <summary>
/// Holds the blue component of the colour
/// </summary>
[FieldOffset(0)]
public byte Blue;
/// <summary>
/// Holds the green component of the colour
/// </summary>
[FieldOffset(1)]
public byte Green;
/// <summary>
/// Holds the red component of the colour
/// </summary>
[FieldOffset(2)]
public byte Red;
/// <summary>
/// Holds the alpha component of the colour
/// </summary>
[FieldOffset(3)]
public byte Alpha;
/// <summary>
/// Permits the color32 to be treated as an int32
/// </summary>
[FieldOffset(0)]
public int ARGB;
/// <summary>
/// Return the color for this Color32 object
/// </summary>
public Color Color
{
get { return Color.FromArgb(Alpha, Red, Green, Blue); }
}
}
}
}
The compiler is warning you that quantit
is never initialized and will always be null
.
You probably should initialize it with an instance of a class that derives from ImageManipulation.Quantizer
(you cannot instantiate Quantizer
itself because it's an abstract class):
private static Quantizer quantit = new QuantizerImplementation();
这篇关于现场XXX从未分配到,并且总是有它的默认值空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!