为啥我在尝试保存时得到黑色矩形而不是我的位图
Posted
技术标签:
【中文标题】为啥我在尝试保存时得到黑色矩形而不是我的位图【英文标题】:Why I'm getting black rectangle instead my Bitmap when try to save it为什么我在尝试保存时得到黑色矩形而不是我的位图 【发布时间】:2014-09-09 06:02:06 【问题描述】:所以问题是为什么我像这样保存我的位图时总是得到黑色矩形
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 保存时没有问题
【问题讨论】:
粘贴你在 myBitmap 中的代码 这一行不是问题,您的位图没有正确创建 @Sayse 请查看更新 @DeepakSharma 请查看更新 这是相当多的代码来尝试猜测哪里出了问题,但gr.FillRectangle(Black..)
似乎很好奇..(如果你把它改成不同的颜色,它会用这种新颜色保存它吗?)
【参考方案1】:
尝试使用这种方式保存:
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);
【讨论】:
什么是 WritebleBitamp?以上是关于为啥我在尝试保存时得到黑色矩形而不是我的位图的主要内容,如果未能解决你的问题,请参考以下文章
在 Windows Metro 下使用 DirectX 绘制位图
当我在我的 android 应用程序中从图库中加载图像时,为啥位图返回较小的图像?