请帮助我解决位图参数在ASP.NET中无效的异常 [英] Pls help me solve bitmap parameter is not valid exception in ASP.NET
本文介绍了请帮助我解决位图参数在ASP.NET中无效的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Here is my code:
public void btnExtract_Click(object sender, System.EventArgs e)
{
MemoryStream message = new MemoryStream();
FileStream key = new FileStream(@"E:\studies\sem8 projects\count.txt", FileMode.Open);
PaletteUtility util = new PaletteUtility(Server.MapPath(@"~/mini/" + upload.FileName),null);
util.Extract(message, key);
message.Seek(0, SeekOrigin.Begin);
StreamReader reader = new StreamReader(message);
Label3.Text = reader.ReadToEnd();
String ch = Label3.Text;
if (ch == "hello")
Response.Write("correct image");
reader.Close();
key.Close();
}
}
public class PaletteUtility
{
public String sourceFileName;
private String destinationFileName;
public long CountUseableUnits(Stream keyStream)
{
long countUseableUnits = 0;
long unitIndex = 0;
byte key;
Bitmap bmp = new Bitmap(sourceFileName);
long countUnits = bmp.Width * bmp.Height;
bmp.Dispose();
while (true)
{
key = GetKey(keyStream);
if (unitIndex + key < countUnits)
{
unitIndex += key;
countUseableUnits++;
}
else
{
break;
}
}
keyStream.Seek(0, SeekOrigin.Begin);
return countUseableUnits;
}
public void Hide(int maxPaletteSize, Stream messageStream, Stream keyStream)
{
//load the original image
Bitmap bmp = new Bitmap(sourceFileName);
ArrayList newPalette = null; //receives the stretched palette
Hashtable colorIndexToNewIndices = null; //recevies the list of new color indices
//create a new palette from the existing one
StretchPalette(bmp.Palette, maxPaletteSize, ref newPalette, ref colorIndexToNewIndices);
//create a bitmap with the new palette and the hidden message
Bitmap newBmp = CreateBitmap(bmp, newPalette, colorIndexToNewIndices, messageStream, keyStream);
//save the new bitmap
newBmp.Save(destinationFileName);
newBmp.Dispose();
bmp.Dispose();
}
public void Extract(Stream messageStream, Stream keyStream)
{
System.Drawing.Image image;
image = BitmapFromWeb(@sourceFileName);
Bitmap bmp = new Bitmap(image);//Here i am getting Parameter is not valid exception
BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);
//copy all pixels
byte[] pixels = new byte[bmpData.Stride * bmpData.Height];
Marshal.Copy(bmpData.Scan0, pixels, 0, pixels.Length);
Color[] palette = bmp.Palette.Entries;
byte messageByte = 0, messageBitIndex = 0, pixel = 0;
int messageLength = 0, pixelIndex = 0;
//read pixels until the message is complete
while ((messageLength == 0) || (messageStream.Length < messageLength))
{
//locate the next pixel that carries a hidden bit
pixelIndex += GetKey(keyStream);
pixel = pixels[pixelIndex];
if ((palette[pixel].B % 2) == 1)
{
//odd blue-component: message-bit was "1"
messageByte += (byte)(1 << messageBitIndex);
} //else: messageBit was "0", nothing to do
if (messageBitIndex == 7)
{ //a byte is complete
//save and reset messageByte, reset messageBitIndex
messageStream.WriteByte(messageByte);
messageBitIndex = 0;
messageByte = 0;
if ((messageLength == 0) && (messageStream.Length == 4))
{
//message's length has been read
messageStream.Seek(0, SeekOrigin.Begin);
messageLength = new BinaryReader(messageStream).ReadInt32();
messageStream.SetLength(0);
}
}
else
{
messageBitIndex++; //next bit
}
}
//release the carrier bitmap
bmp.UnlockBits(bmpData);
bmp.Dispose();
}
我的尝试:
我只是试图从上传的图像文件中提取消息。但它在位图构造函数中显示参数无效异常
What I have tried:
I'm just trying to extract the message from the uploaded image file.But it is showing parameter is not valid exception in the bitmap constructor
推荐答案
首先检查文件本身:通常是因为当它被保存时,它被严重保存,并且它不是真正的图像文件。
看看这个:为什么我得到参数无效。 我从数据库中读取图像时出现异常? [ ^ ] - 它基于数据库存储的图像(因为这是问题最常发生的时候)但是要好好看看如何保存因为你很可能遇到同样的问题!
Start by checking the file itself: normally it's because when it was saved, it was saved badly, and it isn't a "real" image file.
Have a look at this: Why do I get a "Parameter is not valid." exception when I read an image from my database?[^] - it's based on DB stored images (because that's when the problem mostusually occurs) but have a good look at how you save the file as you quite likely have the same problem!
这篇关于请帮助我解决位图参数在ASP.NET中无效的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文