我正在创建一个Bitmap图像并将文件保存到我的硬盘中,并在我的图片框中显示BPM图像。问题是,当我尝试转换pdf中的bmp图像时,它会给我异常The process cannot access the file 'D:\Project\parcelHub\Source\Temp\DefaultShip.jpg' because it is being used by another process.
。下面是我的代码,用于将文件转换为jpeg,然后将转换文件转换为pdf。
Convert to BMP
image = Base64ToImage(p.LabelImage.GraphicImage);
Bitmap rotateimage;
rotateimage = new Bitmap(image);
_bmp = RotateImage(rotateimage, 90);
string appDirectory = Path.GetDirectoryName(Application.ExecutablePath);
appDirectory = appDirectory + @"\Temp";
string pdfFilePath = appDirectory + @"\DefaultShip.jpg";
_bmp.Save(pdfFilePath, System.Drawing.Imaging.ImageFormat.Jpeg);
if (_bmp.Width < pictureBox1.Width && _bmp.Height < pictureBox1.Height)
{
pictureBox1.SizeMode = PictureBoxSizeMode.Normal;
pictureBox1.Image = _bmp;
}
else
{
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
pictureBox1.Image = _bmp;
}
For create pdf
private void bttnConvert_Click(object sender, EventArgs e)
{
Document doc = new Document(iTextSharp.text.PageSize.A4);
try
{
string appDirectory = Path.GetDirectoryName(Application.ExecutablePath);
appDirectory = appDirectory + @"\Temp";
string pdfFilePath = appDirectory + @"\DefaultShip.jpg";
FileStream stream = File.Open(pdfFilePath, FileMode.Open, FileAccess.ReadWrite, FileShare.None);
PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream(pdfFilePath, FileMode.Create));
doc.Open();
iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(pdfFilePath);
jpg.Alignment = Element.ALIGN_CENTER;
doc.Add(jpg);
}
catch (DocumentException docEx)
{
}
catch (IOException ioEx)
{
}
catch (Exception ex)
{
}
finally
{
doc.Close();
}
}
Rotation image
private Bitmap RotateImage(Bitmap b, float Angle)
{
// The original bitmap needs to be drawn onto a new bitmap which will probably be bigger
// because the corners of the original will move outside the original rectangle.
// An easy way (OK slightly 'brute force') is to calculate the new bounding box is to calculate the positions of the
// corners after rotation and get the difference between the maximum and minimum x and y coordinates.
float wOver2 = b.Width / 2.0f;
float hOver2 = b.Height / 2.0f;
float radians = -(float)(Angle / 180.0 * Math.PI);
// Get the coordinates of the corners, taking the origin to be the centre of the bitmap.
PointF[] corners = new PointF[]{
new PointF(-wOver2, -hOver2),
new PointF(+wOver2, -hOver2),
new PointF(+wOver2, +hOver2),
new PointF(-wOver2, +hOver2)
};
for (int i = 0; i < 4; i++)
{
PointF p = corners[i];
PointF newP = new PointF((float)(p.X * Math.Cos(radians) - p.Y * Math.Sin(radians)), (float)(p.X * Math.Sin(radians) + p.Y * Math.Cos(radians)));
corners[i] = newP;
}
// Find the min and max x and y coordinates.
float minX = corners[0].X;
float maxX = minX;
float minY = corners[0].Y;
float maxY = minY;
for (int i = 1; i < 4; i++)
{
PointF p = corners[i];
minX = Math.Min(minX, p.X);
maxX = Math.Max(maxX, p.X);
minY = Math.Min(minY, p.Y);
maxY = Math.Max(maxY, p.Y);
}
// Get the size of the new bitmap.
SizeF newSize = new SizeF(maxX - minX, maxY - minY);
// ...and create it.
Bitmap returnBitmap = new Bitmap((int)Math.Ceiling(newSize.Width), (int)Math.Ceiling(newSize.Height));
// Now draw the old bitmap on it.
using (Graphics g = Graphics.FromImage(returnBitmap))
{
g.TranslateTransform(newSize.Width / 2.0f, newSize.Height / 2.0f);
g.RotateTransform(Angle);
g.TranslateTransform(-b.Width / 2.0f, -b.Height / 2.0f);
g.DrawImage(b, 0, 0);
}
return returnBitmap;
}
答案 0 :(得分:1)
位图是一次性的。
实际上它来自Image,它是一次性的。 (链接到msdn:http://msdn.microsoft.com/en-us/library/system.drawing.bitmap.aspx)
使用using语句
(var rotateimage = new Bitmap(image))
{
//.. your code
}