c#图像处理 未处理securityexception

roar1987 2010-03-12 07:12:33
做了一个关于图像放缩的程序
Rectangle rect = new Rectangle(0, 0, curBitmap.Width, curBitmap.Height);
BitmapData bmpData = curBitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
curBitmap.PixelFormat);
IntPtr ptr = bmpData.Scan0;
int bytes = curBitmap.Width*curBitmap.Height;
byte[] grayValues = new byte[bytes];
System.Runtime.InteropServices.Marshal.Copy(ptr,grayValues,0,bytes);

//得到横向和纵向的缩放量
double x = 5;
double y = 5;

//图像的几何中心
int halfWidth = (int) (curBitmap.Width/2);
int halfHeight = (int) (curBitmap.Height/2);

int xz = 0;
int yz = 0;
int tempWidth = 0;
int tempHeight = 0;
byte[] tempArray = new byte[bytes];

//最临近插值法
for(int i = 0;i < curBitmap.Width;i++)
{
for(int j = 0;j < curBitmap.Height;j++)
{
//以图像的几何中心为坐标原点进行坐标转换
//按逆向映射法得到输入图像的坐标
tempHeight = i - halfHeight;
tempWidth = j - halfWidth;

//在不同的象限进行四舍五入处理
if(tempWidth > 0)
{
xz = (int) (tempWidth/x + 0.5);
}
else
{
xz = (int) (tempWidth/x - 0.5);
}
if(tempHeight > 0)
{
yz = (int) (tempHeight/y + 0.5);
}
else
{
yz = (int) (tempHeight/y - 0.5);
}

//坐标逆变换
tempWidth = xz + halfWidth;
tempHeight = yz + halfHeight;
//得到输出图像像素值
if(tempWidth < 0 || tempWidth >= curBitmap.Width || tempHeight < 0 || tempHeight >= curBitmap.Height)
{
//缩放后留下的空白部分用白色像素代替
tempArray[i*curBitmap.Width + j] = 255;
}
else
{
tempArray[i*curBitmap.Width + j] = grayValues[tempHeight*curBitmap.Width + tempWidth];
}
}
}

调试的时候报出了这个错误:
请求“System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”类型的权限已失败。
应该是红色部分除了问题
之前没有做过图像操作部分的代码,请高手相助!
...全文
135 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
roar1987 2010-03-25
  • 打赏
  • 举报
回复
前些日子有一个考试在准备,没能够及时回帖,感谢您的回复
xingyuebuyu 2010-03-12
  • 打赏
  • 举报
回复

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Imaging;

namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
//work();
}

void work()
{
while (true)
{
backgroundWorker1.RunWorkerAsync();
//backgroundWorker1.ReportProgress
}

}

void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{

}

private Bitmap curBitmap = new Bitmap(600, 800);

private void Form1_Load(object sender, EventArgs e)
{
Image image = curBitmap;
Graphics graphics = Graphics.FromImage(image);
pictureBox1.Image = image;

float size = Convert.ToSingle(0.5);
Pen pen = new Pen(Color.Black, size);

int row = 600;
int col = 800;

for (int i = 0; i < row; i++)
{
graphics.DrawLine(pen, 0, 0 + 5 * i, 600, 0 + 5 * i);
}
for (int j = 0; j < col; j++)
{
graphics.DrawLine(pen, 0 + 5 * j, 0, 0 + 5 * j, 800);
}


}

private void button1_Click(object sender, EventArgs e)
{
Rectangle rect = new Rectangle(0, 0, curBitmap.Width, curBitmap.Height);
BitmapData bmpData = curBitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
curBitmap.PixelFormat);
IntPtr ptr = bmpData.Scan0;
int bytes =bmpData.Stride * curBitmap.Height;
byte[] grayValues = new byte[bytes];
System.Runtime.InteropServices.Marshal.Copy(ptr, grayValues, 0, bytes);

//得到横向和纵向的缩放量
double x = 5;
double y = 5;

//图像的几何中心
int halfWidth = (int)(curBitmap.Width / 2);
int halfHeight = (int)(curBitmap.Height / 2);

int xz = 0;
int yz = 0;
int tempWidth = 0;
int tempHeight = 0;
byte[] tempArray = new byte[bytes];

//最临近插值法
for (int i = 0; i < curBitmap.Width; i++)
{
for (int j = 0; j < curBitmap.Height; j++)
{
//以图像的几何中心为坐标原点进行坐标转换
//按逆向映射法得到输入图像的坐标
tempHeight = i - halfHeight;
tempWidth = j - halfWidth;

//在不同的象限进行四舍五入处理
if (tempWidth > 0)
{
xz = (int)(tempWidth / x + 0.5);
}
else
{
xz = (int)(tempWidth / x - 0.5);
}
if (tempHeight > 0)
{
yz = (int)(tempHeight / y + 0.5);
}
else
{
yz = (int)(tempHeight / y - 0.5);
}

//坐标逆变换
tempWidth = xz + halfWidth;
tempHeight = yz + halfHeight;
//得到输出图像像素值
if (tempWidth < 0 || tempWidth >= curBitmap.Width || tempHeight < 0 || tempHeight >= curBitmap.Height)
{
//缩放后留下的空白部分用白色像素代替
tempArray[i * curBitmap.Width + j] = 255;
}
else
{
tempArray[i * curBitmap.Width + j] = grayValues[tempHeight * curBitmap.Width + tempWidth];
}
}
}

curBitmap.UnlockBits(bmpData);

}
}
}


我这里是没问题的,你是不是因为button点击第二次的时候出的问题?

因为你没调用curBitmap.UnlockBits(bmpData);
所有的数据还是lock状态,你要unlock才可以的
roar1987 2010-03-12
  • 打赏
  • 举报
回复
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
这个数主函数,异常出在上面红色的地方
上面的代码是一个Button 的响应函数 Button的作用是缩放
在屏幕上放了一个picture作为绘图的容器 在窗口的load里面 绘制了一些格子
代码如下
private Bitmap curBitmap = new Bitmap(600,800);
private void Form1_Load(object sender, EventArgs e)
{
Image image = curBitmap;
Graphics graphics = Graphics.FromImage(image);
pictureBox1.Image = image;

float size = Convert.ToSingle(0.5);
Pen pen = new Pen(Color.Black, size);

int row = 600;
int col = 800;

for (int i = 0; i < row; i++)
{
graphics.DrawLine(pen, 0, 0 + 5 * i, 600, 0 + 5 * i);
}
for (int j = 0; j < col; j++)
{
graphics.DrawLine(pen, 0 + 5 * j, 0, 0 + 5 * j, 800);
}
}
private void button2_Click(object sender, EventArgs e)
{}该函数内部是本贴的那部分代码
xingyuebuyu 2010-03-12
  • 打赏
  • 举报
回复
贴完整代码看看

你单步调试是到哪一步出错的?
roar1987 2010-03-12
  • 打赏
  • 举报
回复
谢谢您的回复 为什么我那样写报出的异常是在主函数里面呢 就在该窗口运行的地方?我想知道这是什么错误
如果单纯的是数据空间计算错误 是不是不该这样 还请解答 多谢
xingyuebuyu 2010-03-12
  • 打赏
  • 举报
回复

int bytes = bmpData.Stride*curBitmap.Height;
byte[] grayValues = new byte[bytes];
System.Runtime.InteropServices.Marshal.Copy(ptr,grayValues,0,bytes);


这样改,你那样算的数据空间是不对的,看下别人的解释

http://blog.csdn.net/ki1381/archive/2007/01/10/1478611.aspx

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧