【问题标题】:how to close Bitmap image after save保存后如何关闭位图图像
【发布时间】:2014-07-10 09:35:13
【问题描述】:

我正在创建一个位图图像并将文件保存到我的硬盘驱动器中,并且还在我的图片框中显示 BPM 图像。问题是,当我尝试将 bmp 图像转换为 pdf 时,它给了我异常 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;
    }

【问题讨论】:

    标签: .net winforms c#-4.0


    【解决方案1】:

    位图是一次性的。

    其实它来源于Image,它是一次性的。 (链接到msdn:http://msdn.microsoft.com/en-us/library/system.drawing.bitmap.aspx

    使用 using 语句

    (var rotateimage = new Bitmap(image))
    {
         //.. your code
    }
    

    【讨论】:

    • 我需要改变什么,因为当我使用 dispose 这不起作用
    • 正如我之前写的,在所有“新位图”上使用 using 块。 RotateBitmap 函数中的 returnBitmap 和 rotateimage 变量。
    • 能否请您转换我以前从未使用过的代码
    猜你喜欢
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 2015-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多