为什么我在尝试保存时会得到黑色矩形而不是我的Bitmap

时间:2014-09-09 06:02:07

标签: c# bitmap save

所以问题是为什么我这样保存我的Bitmap时总会得到黑色矩形

    myBitmap.Save(Path.GetDirectoryName(myFilePath)+"\\temp.bmp",System.Drawing.Imaging.ImageFormat.Bmp);

这有什么问题?

UPD 这是我如何得到我的位图。实际上它是“Interleaved 2 of 5”格式的帖子条码

 Image barcodeImage = new System.Drawing.Bitmap(mImgWidth == 0 ? DEFAULT_WIDTH : mImgWidth, mImgHeight == 0 ? DEFAULT_HEIGHT : mImgHeight);

        using (Graphics gr = Graphics.FromImage(barcodeImage))
        {
            int textStartPosX = (int)((float)BARCODE_TEXT_LEFT_MARGIN * mScaleRatioW);
            int textStartPosY = (int)((float)BARCODE_TEXT_TOP_MARGIN * mScaleRatioH);

            int textRectWidth = (int)((float)BARCODE_TEXT_WIDTH * mScaleRatioW);
            int textRextHeight = (int)((float)BARCODE_TEXT_HEIGHT * mScaleRatioH);
            //float textSize = DEFAULT_TEXT_SIZE + DEFAULT_TEXT_SIZE;
            RectangleF textRect = new RectangleF(textStartPosX, textStartPosY, textRectWidth, textRextHeight);
            gr.SmoothingMode = SmoothingMode.AntiAlias;
            gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
            gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
            gr.DrawString(RUSSIAN_POST, new Font("Helvetica", 7), Brushes.Black, textRect);

            int barCodeStartPosX = (int)(((float)BARCODE_LEFT_MARGIN) * mScaleRatioW);
            int barCodeTopMargin = (int)(((float)BARCODE_TOP_MARGIN) * mScaleRatioH);
            int barCodeHeight = (int)((float)BARCODE_HEIGHT * mScaleRatioH);
            for (int codeidx = 0; codeidx < barcodeString.Length; codeidx++)
            {
                char code = barcodeString[codeidx];

                int barwidth = ((code == 'N') || (code == 'n')) ? (int)((float)1 * mScaleRatioW) : (int)((float)3 * mScaleRatioW);

                if ((code == 'N') || (code == 'W'))
                {
                    gr.FillRectangle(System.Drawing.Brushes.Black, barCodeStartPosX, barCodeTopMargin, barwidth, barCodeHeight);
                }

                barCodeStartPosX += barwidth;
            }

            int numStartPosX = (int)(((float)BARCODE_NUM_LEFT_MARGIN) * mScaleRatioW);
            int numStartPosY = (int)((float)BARCODE_NUM_TOP_MARGIN * mScaleRatioH);

            int numRectWidth = (int)((float)BARCODE_NUM_WIDTH * mScaleRatioW);
            int numRextHeight = (int)((float)BARCODE_NUM_HEIGHT * mScaleRatioH);


            gr.SmoothingMode = SmoothingMode.AntiAlias;
            gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
            gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
            RectangleF barCodeNumRect = new RectangleF();
            for (int i = 0; i < 4; i++) {
                float startX;
                switch (i) {
                    case 0: barCodeNumRect = new RectangleF(numStartPosX, numStartPosY, numRectWidth, numRextHeight);
                        gr.DrawString(barcodeNumber.Substring(0,6), new Font("Helvetica", 7), Brushes.Black, barCodeNumRect);
                         break;
                    case 1:
                        numStartPosX = (int)( (numStartPosX + BARCODE_NUM_MONTH_LEFT_PADDING) * mScaleRatioW);
                        barCodeNumRect = new RectangleF(numStartPosX, numStartPosY, numRectWidth, numRextHeight);
                        gr.DrawString(barcodeNumber.Substring(6, 2), new Font("Helvetica", 7), Brushes.Black, barCodeNumRect);
                        break;
                    case 2:
                        numStartPosX = (int)((numStartPosX + BARCODE_NUM_VALUE_LEFT_PADDING) * mScaleRatioW);
                        barCodeNumRect = new RectangleF(numStartPosX, numStartPosY, numRectWidth, numRextHeight);
                        gr.DrawString(barcodeNumber.Substring(8, 5), new Font("Helvetica", 7, FontStyle.Bold), Brushes.Black, barCodeNumRect);
                        break;
                    case 3:
                        numStartPosX = (int)((numStartPosX + BARCODE_NUM_CHECKDIGIT_LEFT_PADDING) * mScaleRatioW);
                        barCodeNumRect = new RectangleF(numStartPosX * mScaleRatioW, numStartPosY, numRectWidth, numRextHeight);
                        gr.DrawString(barcodeNumber.Substring(13, 1), new Font("Helvetica", 7), Brushes.Black, barCodeNumRect);
                        break;
                    default: break;
                }
            }


        }

UPD 2 在没有System.Drawing.Imaging.ImageFormat.Bmp

的情况下保存时没有问题

1 个答案:

答案 0 :(得分:2)

尝试使用这种方式保存:

WriteableBitmap eb = new WriteableBitmap(bitmapImage);
MemoryStream memoryStream1 = new MemoryStream();
eb.SaveJpeg(memoryStream1, bitmapImage.PixelWidth, bitmapImage.PixelHeight, 0, 100);
memoryStream1.Seek(0, SeekOrigin.Begin);
MediaLibrary library1 = new MediaLibrary();
string filename1 = "SavedPicture_" + DateTime.Now.ToString("y_M_d_H_m_s");
Picture pic1 = library1.SavePicture(filename1, memoryStream1);